Tag: generics

为什么C#中的隐式类型转换失败?

背景: 我们假设我有以下课程: class Wrapped : IDisposable { public Wrapped(T obj) { /* … */ } public static implicit operator Wrapped(T obj) { return new Wrapped(obj); } public void Dispose() { /* … */ } } 如您所见,它为T → Wrapped提供了隐式类型转换运算符。 最后,我希望能够使用这个类如下: interface IX { /* … */ } class X : IX { /* … */ } […]

C#中的重载分辨率,扩展方法和通用性

我在C#源代码中有以下场景: class A{} class Dispatch{} static class DispatchExt { public static void D(this Dispatch d, int a) { Console.WriteLine(“Generic D chosen with a = ” + a.ToString()); } public static void D(this Dispatch d, int a) { Console.WriteLine(“D chosen with a = ” + a.ToString()); } } class Program { static void D(Dispatch d, int […]

MEF通用import

我有以下使用MEF的示例代码: public interface IFoo {} public class Foo : IFoo {} [Export(typeof(IFoo))] public class Foo : Foo {} public class Bar { [Import] private readonly IFoo foo; } static void Main() { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); var container = new CompositionContainer(catalog); container.ComposeParts(); var bar = new Bar(); //bar.foo would be null } […]

结构图和generics类型

我的情况与我见过的其他情况有点不同。 为了澄清,这不是正常的问题,例如; 类似于IAClass映射到AClass等 – 这涉及到每个接口基本上使用一个具体的类。 这涉及到一个通用类,但我希望能够加载它的所有可能用法。 例如 – 主要类是 public class MyClass : IMyClass where TDomainObject : DomainObject 这样的例子用法就是 IMyClass p = new MyClass; IMyClass p = new MyClass; 即对于所有DomainObjects,我希望能够加载MyClass 。 所以你可以看到我没有为每个声明使用特定的类,它们都使用相同的类。 我如何将其加载到StructureMap中?

动态转换为generics类型

在周末开始之​​前,请快点… 我有一个带有以下签名的方法,我需要调用: public interface IObjectProvider { T Get(Predicate condition); } 这将为我提供符合谓词标准的任何来源的T 现在,必须从我所拥有的上下文调用以下内容: //the actual predicate that’s going to be evaluated var predicate = predicateProperty.GetValue(invocation.InvocationTarget, null); //The type that should go into the call as type param Type relationTargetType = relationDefinition.RelatedType; 正如您可能猜到的,编译器不会让我使用predicate变量作为参数。 我需要做的是将此对象转换为谓词,但通用类型参数必须是编译时常量,所以这不起作用。 我已经开始搞乱这个,但到目前为止没有成功: Type genericPredicateType = typeof(Predicate); Type specificPredicateType= genericPredicateType.MakeGenericType(relationTargetType); Convert.ChangeType(predicate, specificPredicateType) 我怎么能把它混在一起呢? 编辑:我认为这是一个与用例无关的问题,但显然我错了。 所以,既然对我所做的事情,我拥有的以及为什么以及诸如此类的事情有这样的大惊小怪,这里有更多的背景信息。 […]

创建通用实例

我有一个实现命令模式的WCF服务。 使用reflection,我创建了一个Dictionary,其中键是命令类型,值是CommandHandler。 我们的想法是从WCF接收命令,使用字典获取处理程序类型,然后使用激活器创建处理程序的实例。 public CommandResponse RunCommand(Command command) { _logger.Trace(“Running Command”); var handlerType = HandlerMap[command.GetType()]; var handler = (AbstractCommandHandler)Activator.CreateInstance(handlerType); handler.HandleCommand(command); return new PostStatCommandResponse(); } public class StatCommandHandler : AbstractCommandHandler { public override void HandleCommand(PostStatCommand command) { } } 问题是Activator.CreateInstance返回一个对象,而不是一个强类型的commandhandler。 我需要能够调用HandleCommand,但无法弄清楚如何将它转换为基础AbstractCommandHandler // Syntax error. Gotta provide type to generic (AbstractCommandHandler)Activator.CreateInstance(handlerType); // Casting error at run time (AbstractCommandHandler)Activator.CreateInstance(handlerType); […]

通用约束排除

很抱歉问愚蠢的问题 是否有可能以这样的方式强制执行约束,即给定的T可以从任何引用类型派生,除了一些A,B,C(其中A,B,C是引用类型)。 (即) Where T : class except A,B,C

如何在Unity中检查游戏对象是否有组件方法?

我正在编写一个方法来检查gameObject是否有一个组件。 这里是: public static bool HasComponent (this GameObject obj) { return obj.GetComponent() != null; } 而我正在使用它: void Update() { if (Input.GetKey(“w”)) { if (gameObject.HasComponent()) { print(“Has a rigid body.”); return; } print(“Does not have rigid body.”); } } gameObject没有刚体,但它仍然打印它确实有。

编写具有多种类型的通用扩展方法时的类型推断问题

我正在为IEnumerable编写一个通用扩展方法,用于将对象列表映射到另一个映射对象列表。 这就是我希望该方法工作的方式: IList articles = GetArticles(); return articles.Map(_mappingEngine); 这是方法: public static IEnumerable Map(this IEnumerable list, IMappingEngine engine) { return list.Select(engine.Map); } 但是articles.Map(_mappingEngine); 给出编译错误。 问题是T1的类型推断不起作用。 我必须明确地这样称呼它: articles.Map(_mappingEngine); 如果我创建一个只有一个参数T1的扩展方法,如下所示: public static IEnumerable DummyMap(this IEnumerable list, IMappingEngine engine) { return list; } 然后我可以像这样调用它,而不必指定T1: articles.DummyMap(_mappingEngine); 有没有理由说编译器无法在Map的扩展方法中推断出T1的类型?

Sqlite一对多关系

我正在制作一个需要存储和从sqlite数据库获取数据的应用程序。 我目前使用的两个Nuget包是Sqlite PCL和SQLite-Net Extensions 。 [Table(“patient”)] public class Patient { [PrimaryKey] public string ID { get; set;} public string FirstName { get; set; } public string LastName { get; set; } [OneToMany] public List PatientVitals { get; set; } } [Table(“patientVitals”)] public class PatientVitals { [PrimaryKey] public string VitalID {get;set;} public string Weight { get; […]