Tag: generics

如果在运行时只知道类型参数,如何调用generics方法?

我有这个方法: public List SomeMethod( params ) where T : new() 所以我想把这个SomeMethod ,如果我知道这个类型就好了: SomeMethod(); 但如果我在运行时只有Class1 ,我就无法调用它? 那么如何用未知的T类型调用SomeMethod ? 我用reflection得到了Type。 我有Type类型,但SomeMethod SomeMethod不起作用。 更新7. May: 以下是我想要实现的示例代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication63 { public class DummyClass { } public class Class1 { public string Name; } class AssemblyTypesReflection { static void Main(string[] […]

为什么受“Enum”限制的generics类型无法在C#7.3中被认定为“结构”?

如果我有一个带有struct约束的generics接口,如下所示: public interface IStruct where T : struct { } 我可以提供枚举作为我的类型T ,因为enum满足struct约束: public class EnumIsAStruct : IStruct { } C#7.3添加了一个Enum约束 。 以下代码以前是非法的,现在编译: public class MCVE : IStruct where T : struct, Enum { } 但令我惊讶的是,以下内容无法编译: public class MCVE : IStruct where T : Enum { } ……有错误 CS0453类型’T’必须是非可空值类型才能在generics类型或方法’IStruct’中将其用作参数’T’ 为什么是这样? 我希望由Enum约束的generics类型可用作类型参数,其中类型受struct约束但似乎不是这种情况 – 我必须将我的Enum约束更改为struct, Enum 。 我的期望是错的吗?

Generic方法可以处理Reference和Nullable Value类型吗?

我有一系列的Extension方法来帮助对IDataRecord对象进行空值检查,我目前正在实现这样: public static int? GetNullableInt32(this IDataRecord dr, int ordinal) { int? nullInt = null; return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal); } public static int? GetNullableInt32(this IDataRecord dr, string fieldname) { int ordinal = dr.GetOrdinal(fieldname); return dr.GetNullableInt32(ordinal); } 等等,对于我需要处理的每种类型。 我想重新实现这些作为通用方法,部分是为了减少冗余,部分是为了学习如何编写通用方法。 我写过: public static Nullable GetNullable(this IDataRecord dr, int ordinal) { Nullable nullValue = null; return dr.IsDBNull(ordinal) […]

排除通用约束中的类型(可能?)

是否可以从可能类型的集合中排除特定类型,可以在通用参数中使用? 如果是这样的话。 例如 Foo() : where T != bool 将意味着除bool类型之外的任何类型。 编辑 为什么? 以下代码是我尝试强制执行否定约束。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var x1=Lifted.Lift(“A”); var x2=Lifted.Lift(true); } static class Lifted { // This one is to “exclude” the inferred type variant of the parameter [Obsolete(“The […]

C#将类暴露给COM – 通用集合

我们有一个用C#.Net 2.0编写的小框架,我们想要向COM公开。 问题是,我们有一些通用类,将公开如下: interface IOurClass { ReadonlyCollection OurCollection { get; } } interface IOurListObject { //Some properties that don’t matter } 将通用集合暴露给COM的最佳(或推荐方法)是什么? 我们不必支持generics,我们只需要以某种方式公开IOurListObject的集合。 我们也希望避免为我们使用的每个集合编写新类,但可能无法实现。

ICollection 不协变?

这样做的目的是同步包含图形边缘的两个集合,发送方和接收方,以便在发生某些事情(删除边缘,添加边缘等)时通知双方。 为此,对集合的(反向)引用包含在集合中的元素中 class EdgeBase { EdgeBase(ICollection rCol, ICollection sCol) { RecvCol=rCol; SendCol=sCol; } ICollection RecvCol; ICollection SendCol; public virtual void Disconnect() // Synchronized deletion { RecvCol.Remove(this); SendCol.Remove(this); } } class Edge : EdgeBase { Edge(ICollection rCol, ICollection sCol) : base(rCol, sCol) {} int Weight; } 删除(断开连接)没问题,但在创建过程中出现了问题: HashSet receiverSet, senderSet; var edge = new Edge(receiverSet, senderSet); […]

XML反序列化通用方法

我有下一个XML文件: d639a54f-baca-11e1-8067-001fd09b1dfd -24145 e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd 0.28 我把它反序列化到这个类: [XmlRoot(“Root”, IsNullable = false)] public class DocBalanceCollection { [XmlElement(“Document”)] public List DocsBalanceItems = new List(); } DocBalanceItem是: public class DocBalanceItem { [XmlElement(“Id”)] public Guid DocId { get; set; } [XmlElement(“Balance”)] public decimal? BalanceAmount { get; set; } } 这是我的反序列化方法: public DocBalanceCollection DeserializeDocBalances(string filePath) { var docBalanceCollection = new DocBalanceCollection(); […]

C#不能从generics方法调用重载的非generics方法

我有一些遗留代码,方法foo有700多个重载: [DllImport(“3rdparty.dll”)] protected static extern void foo(int len, ref structA obj); [DllImport(“3rdparty.dll”)] protected static extern void foo(int len, ref structB obj); [DllImport(“3rdparty.dll”)] protected static extern void foo(int len, ref structC obj); //and 700 similar overloads for foo… 我想通过使用generics的单个方法公开这些重载方法: public void callFoo(int len) where T : new() //ensure an empty constructor so it can be activated […]

C#2.0generics:如何创建零参数的Action对象

首先,我使用的是VS2005和C#2.0。 我正在尝试从SelectedIndexChanged事件中设置一个combobox’Text属性。 从StackOverflow上的另一个线程开始,提出了以下方法: BeginInvoke(new Action(() => someCombobox.Text = “x” )); 现在,首先,这将为我返回编译器错误。 我相信这是因为Action对象在两种语言规范中表现不同。 在C#2.0中,Action对象似乎在所有声明中都需要结构。 也许我错了,但我想澄清一下。 工作如下: BeginInvoke(new Action( delegate { someCombobox.Text = “x”; }), new object[] { “” }); 但是,我似乎很奇怪,我必须使用类型参数定义Action对象(特别是因为我不打算传递任何参数)! 以某种方式删除此参数也会使空的新对象[]过时,这就是我想要的。 任何人都可以帮我简化上述电话吗? 最后,是否保证BeginInvoke将在SelectedIndexChanged之后完成,从而使用正确的文本更新combobox’Text属性? 我真的很感激能够学习这些问题的答案。

是否可以在C#中使用未绑定类型作为generics类型参数?

我有一个C#generics: public class Generic { … } 似乎我不能使用未绑定的类型作为类型参数。 我收到error CS1031: Type expected当我尝试以下操作时error CS1031: Type expected : var lGenericInstance = new Generic<List>(); 如何将未绑定类型用作generics类型参数? 有解决方法吗? 我的generics类只是使用reflection,所以我可以获得所提供类型的成员列表作为字符串。 更新:我的答案是关于未绑定类型的问题,因此我已经跟进了一个单独的问题来解决我的具体问题。