Tag: generics

调用generics类的方法

这是上下文: 我尝试编写一个映射器来动态地将我的DomainModel对象转换为ViewModel Ojects。 我得到的问题是,当我尝试通过reflection调用generics类的方法时,我得到了这个错误: System.InvalidOperationException:无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作。 有人可以帮助我找出错误在哪里吗? 这将不胜感激 这是代码(我试图简化它): public class MapClass { public string Test() { return test } public void MapClassReflection(SourceType source, ref DestinationType destination) { Type sourceType = source.GetType(); Type destinationType = destination.GetType(); foreach (PropertyInfo sourceProperty in sourceType.GetProperties()) { string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType); if (destinationPropertyName != null) { PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName); […]

通用类的扩展方法

可能重复: C# – 通用扩展方法 你如何为一般类型的类编写C#扩展方法 是否可以为generics类声明扩展方法? public class NeedsExtension { public NeedsExtension DoSomething(T obj) { // …. } }

XmlSerializer和List 具有默认值

我在序列化时观察到一种奇怪的行为,而不是反序列化具有List类型成员的类,该类在构造时填充了默认值。 与基于数组的属性不同, List类型的属性在XmlSerializer的反序列化时不会被清空。 这是我的代码: public class Program { public class Config { public Config() { Test1 = new List() {“A”, “B”}; Test2 = new String[] {“A”, “B”}; } public List Test1 {get;set;} public string[] Test2 {get;set;} } public static void Main() { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Config)); using(Stream s = new MemoryStream()) { xmlSerializer.Serialize(s, new […]

接口上的C#generics隐式强制转换失败

为什么以下不编译? 当T是接口时,导致编译器认为它无法从Container为T的接口有什么特别之处? 我不认为这是一个协变问题,因为我不是在倾斜,但也许是。 这很像为什么C#编译器不调用隐式转换运算符? 但我不认为它是完全一样的。 Product pIn =null; Product pOut; Container pContainer; List pListIn = null; List pListOut; Container<List> pListContainer; IList pIListIn = null; IList pIListOut; Container<IList> pIListContainer; pContainer = pIn; pOut = pContainer; // all good pListContainer = pListIn; pListOut = pListContainer; // all good too pIListContainer = pIListIn; // fails , cant do […]

C#:从generics方法调用非generics方法

class CustomClass where T: bool { public CustomClass(T defaultValue) { init(defaultValue); // why can’t the compiler just use void init(bool) here? } public void init(bool defaultValue) { } // public void init(int defaultValue) will be implemented later } 你好。 这似乎是一个简单的问题,但我在互联网上找不到答案:为什么编译器不会使用init方法? 我只想为不同类型提供不同的方法。 而是打印以下错误消息:“’CustomClass.init(bool)’的最佳重载方法匹配’有一些无效的参数” 我很乐意提示。 最好的问候,克里斯

在LINQ to Entities(EF4.1)中返回具有类型约束的generics的输入类型

我有一个简单的扩展方法,用于按标签过滤LINQ IQueryable。 我正在使用LINQ to Entities,接口为: public interface ITaggable { ICollection Tags { get; } } 以下不起作用,返回IQueryable而不是IQueryable : public static IQueryable WhereTagged(this IQueryable set, string tag) where T:ITaggable { return set.Where(s=>s.Tags.Any(t=>t.Name.ToLower() == tag.ToLower())); } 这导致LINQ to Entities强制转换exception: “无法将类型’ReleaseGateway.Models.Product’转换为’ReleaseGateway.Models.ITaggable’.LINQ to Entities仅支持转换实体数据模型基元类型。” (System.NotSupportedException)捕获到System.NotSupportedException:“无法将类型’Project.Models.Product’强制转换为’Project.Models.ITaggable’.LINQ to Entities仅支持转换实体数据模型基元类型。” 它没有像这样的约束,但我必须在我的应用程序代码中显式声明类型T: public static IQueryable WhereTagged(this IQueryable set, string tag) { return set.Where(s=>s.Tags.Any(t=>t.Name.ToLower() == […]

C#generics – 为什么lambda工作,简单的方法不工作?

我无法理解为什么C#编译器可以推断出类型 Array.ConvertAll(new int[1], i => Convert.ToDouble(i)); 但不是 Array.ConvertAll(new int[1], Convert.ToDouble); 当看起来前者比后者更复杂的演绎时。 有人可以解释为什么会这样吗?

无法转换类型:为什么必须投两次?

鉴于这个高度简化的例子: abstract class Animal { } class Dog : Animal { public void Bark() { } } class Cat : Animal { public void Mew() { } } class SoundRecorder where T : Animal { private readonly T _animal; public SoundRecorder(T animal) { _animal = animal; } public void RecordSound(string fact) { if (this._animal is […]

enum 是IEnumerable 在generics方法中返回true

这是此问题的后续内容: 对通用枚举集合应用的Cast .Cast 会导致无效的强制转换exception enum Gender { Male, Female } Gender g = Gender.Male; bool b = g is int; // false, alright no issues b = new[] { g } is IEnumerable; // false, alright no issues b = Is(g); //false, alright no issues b = Is<Gender[], IEnumerable>(new[] { g }); // true, why […]

为什么((IList )数组).ReadOnly = True但是((IList)数组).ReadOnly = False?

我知道在.NET中,所有数组都派生自System.Array,System.Array类实现IList , ICollection和IEnumerable 。 实际数组类型还实现IList , ICollection和IEnumerable 。 这意味着如果你有一个String[] ,那么String[]对象也是一个System.Collections.IList和一个System.Collections.Generic.IList ;. 不难看出为什么那些IList会被认为是“ReadOnly”,但令人惊讶的是…… String[] array = new String[0]; Console.WriteLine(((IList)array).IsReadOnly); // True Console.WriteLine(((IList)array).IsReadOnly); // False! 在这两种情况下,尝试通过Remove()和RemoveAt()方法删除项会导致NotSupportedException。 这表明两个表达式都对应于ReadOnly列表,但IList的ReadOnly属性不会返回预期值。 怎么会?