C# 为什么IsAssignableFrom和GetInterface给出不同的结果

C# 为什么IsAssignableFrom和GetInterface给出不同的结果,c#,reflection,inheritance,C#,Reflection,Inheritance,我创建了如下类型: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public, typeof(BaseClass), new Type[] { typeof(ImyInterface) }); 然后为构造函数、方法等生成大量代码。 当我开始使用这个类时,我注意到一些奇怪的事情。我想检查我创建的类型“myname”是否真的实现了ImyInterface。

我创建了如下类型:

TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class |
      TypeAttributes.Public, typeof(BaseClass), new Type[] { typeof(ImyInterface) });
然后为构造函数、方法等生成大量代码。 当我开始使用这个类时,我注意到一些奇怪的事情。我想检查我创建的类型“myname”是否真的实现了ImyInterface。我希望以下两种说法都是正确的:

// t is Type 'myName'
Type baseInterface = t.GetInterface(typeof(ImyInterface).name);   
if (baseType != null)
{
  // this is actually true, as I expected
}

if (typeof(ImyInterface).isAssignableFrom(t))
{
  // the if clause is false, but I don't have a clue why??
}
因此,我创建了一个实现ImyInterface的类,但它不能分配给ImyInterface类型的对象,我缺少什么

顺便说一下,没有涉及泛型,接口只是测试概念的基本接口:

public interface ITestInterface
{
    int CalcSquaredInteger(int number);
}

这是可行的,所以我想问题出在你们并没有在这里发布的代码中


编辑:已更新,因此IMyInterface现在位于另一个项目中,并且仍然有效

我终于找到了我所缺少的内容:无论何时检查在不同项目/程序集中定义的类型和接口之间的类型兼容性和可分配性,请确保所有项目都已签名并具有强名称!!否则GetInterface方法将起作用,因为它只是比较一个名称。但是.net不会在类型之间赋值。

是否涉及泛型?接口是如何声明的?没有泛型,我在问题中添加了示例接口。上面代码中的“ImyInterface”是什么?考虑到你正在调用一个属性“name”,它不可能只是一个接口,对吗?你是对的,它应该是typeof(),name是一个类型的属性。始终确保复制并粘贴有问题的实际生产代码。如果您必须简化,请确保它在发布之前编译并显示相同的问题。问题太多了,所以代码显示的问题与问题所在的问题完全不同。“这是可行的,所以我猜问题出在代码中的某个地方,你没有在这里发布”,这个结论当然是正确的。奇怪的是,我的代码与您的示例相似。唯一的区别是,在我的示例中,接口类型(在您的示例中为IMyInterface)是执行整个程序集/类型创建的方法的参数。这种类型是在另一个名称空间/项目中定义的,所以我在那里挖掘,看看这是否有区别。当我剪切并粘贴接口并在当前名称空间中硬编码它时,它就可以工作了,但这不是我需要的…我的猜测是,强名称就足够了,在您的场景中签名应该不重要。顺便说一句:我注意到IsAssignableFrom的实现在.NET3.5和4.0中有很大的不同。你用的是什么版本?你是对的,强名称就足够了!我用的是3.5,谢谢你指出这一点。我将在我的待办事项列表中检查4.0版本中的行为
using ClassLibrary1; // this is another project that contains IMyInterface

namespace ConsoleApplication1
{
    public class MyBaseClass
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyReflectionTest(typeof(ClassLibrary1.IMyInterface));
        }

        private static void MyReflectionTest(Type interfaceType)
        {

            AssemblyName aName = new AssemblyName("DynamicAssemblyExample");
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave);

            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");

            TypeBuilder tb = mb.DefineType("MyDynamicType", TypeAttributes.Public, typeof(MyBaseClass), new Type[] { interfaceType });

            MethodBuilder mbIM = tb.DefineMethod("IMyInterface.MyTestMethod", MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final, null, Type.EmptyTypes);
            ILGenerator il = mbIM.GetILGenerator();
            il.Emit(OpCodes.Ret);

            tb.DefineMethodOverride(mbIM, interfaceType.GetMethod("MyTestMethod"));

            var myType = tb.CreateType();

            Debug.Assert(interfaceType.IsAssignableFrom(myType) == true);
        } 
    }
}