C# Can';t调用接受不同程序集中定义的类型参数的函数
假设有两个程序集是用C#编写的。第一个程序集的代码为:C# Can';t调用接受不同程序集中定义的类型参数的函数,c#,.net,assemblies,ironpython,C#,.net,Assemblies,Ironpython,假设有两个程序集是用C#编写的。第一个程序集的代码为: namespace VSInterface { public class X { } } namespace VSInterface { public class TestIPY { public static void foo(X bar) { } } } 第二个程序集的代码为: namespace VSInterface {
namespace VSInterface
{
public class X
{
}
}
namespace VSInterface
{
public class TestIPY
{
public static void foo(X bar)
{
}
}
}
第二个程序集的代码为:
namespace VSInterface
{
public class X
{
}
}
namespace VSInterface
{
public class TestIPY
{
public static void foo(X bar)
{
}
}
}
当我从以下IPY代码调用函数foo时:
import clr
clr.AddReferenceToFileAndPath(r"SomeFolder\FirstAssembly.dll")
clr.AddReferenceToFileAndPath(r"SomeOtherFolder\SecondAssembly.dll")
from VSInterface import TestIPY, X
TestIPY.foo(X())
我得到一个错误:
Traceback (most recent call last):
File "test.py", line 6, in <module>
TypeError: expected X, got X
我真的不明白为什么在使用相对路径时它不起作用,我也不确定这是否是IronPython中的错误。当程序集
FirstAssembly.dll
位于文件夹:SomeFolder
和SomeOtherFolder
中时,就会出现这个问题。因为在这种情况下,类型X
get被加载了两次,并且它与foo
SomeFolder
和SomeOtherFolder
的符号不再匹配,SomeOtherFolder
是否包含它自己的FirstAssembly.dll
?它可能作为SecondAssembly.dll
的依赖项加载了另一个。您必须在运行时创建引用吗?如果您使用的是VS Studio,您可能会使用[Add Reference]功能,并在编译时内置引用,然后使用“using”语句而不是“import”。这可以解决你的问题。我还注意到两者都有相同的名称空间。您是否尝试更改一个文件夹的命名空间以查看是否仍然存在问题?@JamesThorpe是的,SomeOtherFolder
包含FirstAssembly.dll
的副本,这就是问题的原因。看本德的答案,你是对的。“SomeOtherFolder”包含“FirstAssembly.dll”的副本,因为Visual Studio中的“CopyLocal”选项已设置为true。在我删除了.dll文件后,它可以正常工作。谢谢