Tag: generics

如果直到运行时才知道类型,如何创建Expression.Lambda?

最好使用代码解释。 我有一个generics类,它有一个返回整数的方法。 这是一个简单的版本,用于解释… public class Gen { public int DoSomething(T instance) { // Real code does something more interesting! return 1; } } 在运行时,我使用reflection来发现某事物的类型,然后想要为该特定类型创建我的Gen类的实例。 这很容易,像这样做…… Type fieldType = // This is the type I have discovered Type genericType = typeof(Gen).MakeGenericType(fieldType); object genericInstance = Activator.CreateInstance(genericType); 我现在想要创建一个Expression,它将参数作为generics类型的一个实例,然后调用该类型的DoSomething方法。 所以我希望Expression能有效地执行此操作…… int answer = genericInstance.DoSomething(instance); …除了我之前在运行时稍后没有’实例’并且genericInstance是生成的类型,如上所示。 我为此创建Lambda的尝试如下…… MethodInfo mi = […]

通用接口的通用列表是不允许的,任何替代方法?

我试图找到使用通用接口通用列表作为变量的正确方法。 这是一个例子。 它可能不是最好的,但希望你能明白这一点: public interface IPrimitive { T Value { get; } } 然后在另一个类中,我希望能够声明一个变量,该变量包含对任意T实现IPrimitive的对象列表。 // I know this line will not compile because I do not define T List<IPrimitive> primitives = new List<IPrimitives>; primitives.Add(new Star()); // Assuming Star implements IPrimitive primitives.Add(new Sun()); // Assuming Sun implements IPrimitive 请注意,对于列表中的每个条目, IPrimitive可能不同。 关于如何建立这种关系的任何想法? 替代方法?

entity framework通用存储库错误

我正在尝试为我的Entity Framework存储库创建一个非常通用的generics存储库,该存储库具有基本的CRUD语句并使用接口。 我先打了一个砖墙头,然后被打倒了。 这是我的代码,使用entity framework模型在控制台应用程序中编写,其中包含名为Hurl的表。 只需尝试通过其ID撤回对象。 这是完整的应用程序代码。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Objects; using System.Linq.Expressions; using System.Reflection; using System.Data.Objects.DataClasses; namespace GenericsPlay { class Program { static void Main(string[] args) { var hs = new HurlRepository(new hurladminEntity()); var hurl = hs.Load(h => h.Id == 1); Console.Write(hurl.ShortUrl); Console.ReadLine(); } } public interface IHurlRepository […]

EqualityComparer 。默认与T.Equals

假设我有一个通用的MyClass需要比较两个类型的对象。 通常我会做…… void DoSomething(T o1, T o2) { if(o1.Equals(o2)) { … } } 现在假设我的MyClass有一个支持传递自定义IEqualityComparer的构造函数,类似于Dictionary 。 在那种情况下,我需要做…… private IEqualityComparer _comparer; public MyClass() {} public MyClass(IEqualityComparer comparer) { _comparer = comparer; } void DoSomething(T o1, T o2) { if((_comparer != null && _comparer.Equals(o1, o2)) || (o1.Equals(o2))) { … } } 要删除这个冗长的if语句,如果使用常规构造函数,我可以将_comparer默认为’default _comparer ‘。 我搜索了类似typeof(T).GetDefaultComparer()但却无法找到类似的东西。 我确实找到了EqualityComparer.Default ,我可以使用吗? 然后这个片段…… […]

如何在字典集合中查找项目?

我已经声明并填充了以下集合。 protected static Dictionary _tags; 现在我想查找集合中的特定条目。 我尝试了以下内容。 thisTag = _tags.FirstOrDefault(t => t.Key == tag); if (thisTag != default(KeyValuePair)) … 我收到错误: 运算符’!=’不能应用于’System.Collections.Generic.KeyValuePair’和”类型的操作数 最初我试图将结果与null进行比较,我猜struct不支持。 我会认为在一个集合中找到一个项目是一项非常简单的任务。 那么我如何确定是否找到了我正在寻找的物品? (注意:我正在使用Dictionary因为我想要快速查找。我知道我可以使用Contains()来确定项目是否存在。但这意味着总共有两次查找,这有点挫败了快速查找的目的如果可以快速查找项目,我会愉快地使用不同的集合,我有办法确定它是否成功。)

输入类型的通用方法

我正在尝试编写通用方法来转换类型。 我想写一些像Cast.To(variable)而不是(Type) variable 。 我这个方法的错误版本: public class Cast { public static T To(object o) { return (T) o; } } 这是一个简单的测试: public class A { public static explicit operator B(A a) { return new B(); } } public class B { } A a = new A(); B b = Cast.To(a); 正如您猜测的那样,此代码将因InvalidCastException而失败。 此代码是否失败,因为虚拟机不知道如何在运行时将类型object变量转换为类型B ? 但是exception消息说:“无法将类型A的对象转换为B类”。 […]

改进大型结构列表的二进制序列化性能

我有一个结构,在3个整数中持有3d坐标。 在测试中,我将一个包含100万个随机点的List 组合在一起,然后将二进制序列化用于内存流。 内存流大约是21 MB – 这看起来非常低效,因为1000000点* 3个coords * 4个字节应该在最小11MB时出现 它在我的测试台上也需要约3秒钟。 有关提高性能和/或尺寸的想法吗? (如果有帮助,我不必保留ISerialzable接口,我可以直接写入内存流) 编辑 – 从下面的答案我已经把一个序列化摊牌比较BinaryFormatter,’原始’BinaryWriter和Protobuf using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using ProtoBuf; namespace asp_heatmap.test { [Serializable()] // For .NET BinaryFormatter [ProtoContract] // For Protobuf public class Coordinates : ISerializable { [Serializable()] [ProtoContract] public […]

在C#中:如何声明一个类型为key的通用Dictionary和该类型的IEnumerable 作为值?

我想声明一个字典,用于存储特定类型的类型IEnumerable ,并将其确切类型作为键,如下所示:(编辑后跟随johny g的评论) private IDictionary<Type, IEnumerable> _dataOfType where T: BaseClass; //does not compile! 我想要存储的具体类都是从BaseClass派生的,因此想把它当作约束。 编译器抱怨它希望在成员名称后面加一个分号。 如果它可以工作,我希望这将使后来从字典中检索简单如下: IEnumerable concreteData; _sitesOfType.TryGetValue(typeof(ConcreteType), out concreteData); 如何定义这样的字典?

使用Simple Injector在C#中实现域事件处理程序模式

我正在尝试使用Simple Injector在C#中实现域事件模式。 我已经将我的代码简化为一个文件,该文件可以作为控制台应用程序运行,并且已经排除了Simple Injector代码,以便为此问题保持清晰。 我IEvent的问题是每个事件可能有多个事件处理程序,并且可能会引发多个事件,但我想限制我的Dispatcher只处理实现IEvent接口的事件,所以我把这个限制放在我的Dispatch方法上。 这导致了如何从Simple Injector获取实例的问题,因为每次调用Dispatch方法时TEvent都是IEvent类型(正如我所期望的那样)但是我需要获取传入的事件类型以便我可以获得相关的Simple Injector的处理程序。 希望我的代码能够更好地解释这个: interface IEvent { } interface IEventHandler where T : IEvent { void Handle(T @event); } class StandardEvent : IEvent { } class AnotherEvent : IEvent { } class StandardEventHandler : IEventHandler { public void Handle(StandardEvent @event) { Console.WriteLine(“StandardEvent handled”); } } class AnotherEventHandler : IEventHandler { […]

C#:具有构造函数的generics类型?

我有以下C#测试代码: class MyItem { MyItem( int a ) {} } class MyContainer where T : MyItem, new() { public void CreateItem() { T oItem = new T( 10 ); } } Visual Studio无法编译它,错误是在使用’new’的行: ‘T’: cannot provide arguments when creating an instance of a variable type 在C#中是否可以使用非参数构造函数创建generics类型的对象? 在C ++模板中做这样的事情没有问题,所以我很好奇为什么我不能在C#中做同样的事情。 也许还需要一些额外的’where’或语法不同?