gpt4 book ai didi

c# - C# 中的通用类型构造函数解析,适用于具有通用服务注册的 IoC

转载 作者:行者123 更新时间:2023-11-30 17:23:55 27 4
gpt4 key购买 nike

我正在尝试向我们的 IoC 容器添加通用服务支持,但我有一个问题。

假设我有以下类型:

public interface IService<T> { ... }
public class Service<T> : IService<T> { ... }

然后是下面的代码:

Type type = typeof(Service<>); // <-- notice no specific type here
ConstructorInfo ctor = LogicToFindCtor(type); // <-- picks one of the ctors

现在,有了这个,我有了一个泛型类型和一个我无法调用的构造函数,因为它是通过具有泛型参数的泛型类型获得的。

因此,鉴于我在我的代码中尝试解析特定类型,比如 IService<Employee> ,我可以很容易地通过我的代码找到我需要使用具体类型 Service<T> 来解决这个问题,并且我需要注入(inject)类型 EmployeeT .

但是,我的问题是:鉴于我已经在通用非特定类型上找到了我想要调用的构造函数,我如何找到特定类型的正确构造函数。

Type specificType = typeof(Service<Employee>);
ConstructorInfo specificCtor = MapGenericToSpecific(ctor, specificType);

编辑:我注意到属性 MetaDataToken两个构造函数(来自非特定类型的构造函数和来自特定类型的构造函数)在这里具有相同的值,我可以使用它来匹配吗?它仍然需要一个循环来找到正确的构造函数,但是用一个简单的 if 语句来找到正确的构造函数的循环比查看参数并尝试以这种方式匹配要好得多。我已经更改了下面的代码。现在可以编译运行了,有没有更好的反射更少的方法,也许直接在构造函数表中查找以根据 metadatatoken 值找到构造函数?

代码如下:

using System;
using System.Linq;
using System.Reflection;
using System.Diagnostics;
namespace ConsoleApplication10
{
public class ActivationAttribute : Attribute { }

public class TestClass<T1, T2>
{
public TestClass(String p1)
{
Console.Out.WriteLine("Wrong constructor");
}

[Activation]
public TestClass(T1 p1)
{
Console.Out.WriteLine("Right constructor, p1=" + p1);
}

public TestClass(T2 p2)
{
Console.Out.WriteLine("Wrong constructor");
}

public TestClass()
{
Console.Out.WriteLine("Wrong constructor");
}

public TestClass(T1 p1, T2 p2)
{
Console.Out.WriteLine("Wrong constructor");
}

public TestClass(String p1, T2 p2)
{
Console.Out.WriteLine("Wrong constructor");
}

public TestClass(String p1, Int32 p2)
{
Console.Out.WriteLine("Wrong constructor");
}
}

public class Program
{
static void Main(string[] args)
{
// This is the type I have in my IoC container
Type genericType = typeof(TestClass<,>);

// This is the constructor I locate
ConstructorInfo genericCtor =
(from ctor in genericType.GetConstructors()
where ctor.IsDefined(typeof(ActivationAttribute), false)
select ctor).First();
Debug.Assert(genericCtor != null);

// RESOLUTION STEP

// Upon resolution, two actual types are specified
Type[] genericArguments = new Type[] { typeof(String), typeof(Int32) };

// So I create the actual type from the generic one
Type specificType = genericType.MakeGenericType(genericArguments);

// Can I look up the "genericCtor.MetadataToken" property directly?
// or is this as good as it gets?
ConstructorInfo specificCtor =
(from ctor in specificType.GetConstructors()
where ctor.MetadataToken == genericCtor.MetadataToken
select ctor).First();
Debug.Assert(specificCtor != null);

Debug.Assert(specificCtor != null, "No matching constructors was found");
Object instance = specificCtor.Invoke(new Object[] { "Test" });

Console.Out.Write("Press enter to exit...");
Console.In.ReadLine();
}
}
}

最佳答案

您可以依靠 MetadataToken 在有界和无界构造函数上保持相同。元数据是使用 IL 代码创建的,它是类型的一部分,与通用参数绑定(bind)无关。

.NET 不会为每次使用泛型参数创建不同的类型(例如 List<int>List<bool> )。它是相同的类型,但在每次调用时,有界泛型参数与方法参数一起传递到堆栈上(我知道你可能已经知道所有这些,但我只是在确定)。

因此,当类泛型参数绑定(bind)到特定类型时,附加到类型的 MetadataToken 在运行时不会更改。例如:

bool unboundAndBoundHasSameMetadataToken =
typeof(IList<>).MetadataToken == typeof(IList<int>).MetadataToken;

bool boundedClassesHasSameMetadataToken =
typeof(IList<bool>).MetadataToken == typeof(IList<int>).MetadataToken;

Assert.IsTrue(unboundAndBoundHasSameMetadataToken);
Assert.IsTrue(boundedClassesHasSameMetadataToken);

最重要的是,MetadataToken 是 IL 的“一部分”,并且在具有不同通用参数绑定(bind)的相同类型的实例上没有区别。


出于好奇(我可能遗漏了一些东西),为什么不使用在有界类型上查找 Activation 构造函数的过滤器,就像在未绑定(bind)类型上一样? Activation 属性也装饰了有界构造函数。

关于c# - C# 中的通用类型构造函数解析,适用于具有通用服务注册的 IoC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1588216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com