C# 设置为特定类型(例如typeof(List).GetConstructors())顺便说一句,正确的术语是,正如您在注释中所称的,泛型参数有界/无界的类型构造函数。MetadataToken不依赖于T,在指定T之前,具有特定T的SomeGenericTyp

C# 设置为特定类型(例如typeof(List).GetConstructors())顺便说一句,正确的术语是,正如您在注释中所称的,泛型参数有界/无界的类型构造函数。MetadataToken不依赖于T,在指定T之前,具有特定T的SomeGenericTyp,c#,generics,reflection,C#,Generics,Reflection,设置为特定类型(例如typeof(List).GetConstructors())顺便说一句,正确的术语是,正如您在注释中所称的,泛型参数有界/无界的类型构造函数。MetadataToken不依赖于T,在指定T之前,具有特定T的SomeGenericType(T)与SomeGenericType(T)具有相同的元数据。 public interface IService<T> { ... } public class Service<T> : IService<T&

设置为特定类型(例如typeof(List).GetConstructors())顺便说一句,正确的术语是,正如您在注释中所称的,泛型参数有界/无界的类型构造函数。MetadataToken不依赖于T,在指定T之前,具有特定T的SomeGenericType(T)与SomeGenericType(T)具有相同的元数据。
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
Type specificType = typeof(Service<Employee>);
ConstructorInfo specificCtor = MapGenericToSpecific(ctor, specificType);
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();
        }
    }
}
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);