【发布时间】:2020-11-06 14:01:51
【问题描述】:
想象以下问题:
//Assembly 1
namespace R {
public class Remote
{
public enum SomeTypes
{
A = 12,
B = 14,
C = 16
}
public void DoSomething(SomeTypes s, int a)
{
Console.WriteLine((int)s * a);
}
}
}
//Assembly 2
namespace L
{
public class Local
{
public enum SomeTypes
{
A = 12,
B = 14,
C = 16
}
public Local()
{
assembly = ....;
dynamic instance = assembly.DefinedTypes.First(t => t.GetName() == "R.Remote").GetConstructor(Type.EmptyTypes).Invoke(new object [] {});
instance.DoSomething(SomeTypes.A,3); //this is where it crashes because of an argument mismatch, fair point: One is R.Remote.SomeTypes the other one L.Local.SomeTypes
instance.DoSomething((int)SomeTypes.A,3); //this should work technically, does not, one is R.Remote.SomeTypes the other one int, despite the fact that they can be converted into each other
instance.DoSomething((dynamic)SomeTypes.A,3); //just a hacky guess, but this does not work either
}
}
}
有谁知道如何在不触发参数不匹配异常的情况下调用 DoSomething(...)?
以防万一:我无法访问远程程序集。
提前非常感谢你:)
【问题讨论】:
-
Remote.SomeTypes是一个public枚举,那么为什么要在本地程序集中重新定义它呢? -
“无法访问远程程序集”是什么意思?您正在从中调用
DoSomething(),因此您似乎可以正常访问它。你的意思是你不能修改那个程序集中的代码?Remote是插件还是什么的?我想知道为什么您要在运行时加载它,而不是仅仅将其添加为参考。现在我也有点困惑。 -
@JohnathanBarclay 导致 OP 有点困惑 =)
标签: c# dynamic reflection