首先,对不起,如果以前有人问过这个问题。我已经进行了非常全面的搜索,但没有发现任何类似的东西,但我可能错过了一些东西。
现在问题是:我试图通过反射调用构造函数,但没有运气。基本上,我有一个我想克隆的对象,所以我在复制构造函数中查找它的类型,然后想要调用它。这是我所拥有的:
public Object clone(Object toClone) {
MethodBase copyConstructor = type.GetConstructor(
new Type[] { toClone.GetType() });
return method.Invoke(toClone, new object[] { toClone }); //<-- doesn't work
}
我这样称呼上面的方法:
List<int> list = new List<int>(new int[] { 0, 1, 2 });
List<int> clone = (List<int>) clone(list);
现在,请注意我使用的调用方法是MethodBase
调用。ConstructorInfo
提供了一个调用方法,如果像这样调用它就可以工作:
return ((ConstructorInfo) method).Invoke(new object[] { toClone });
但是,我想使用MethodBase
' 的方法,因为实际上我不是每次都查找复制构造函数,而是将它存储在字典中,而字典包含方法和构造函数,所以它是 a Dictionary<MethodBase>
,而不是Dictionary<ConstructorInfo>
。我当然可以ConstructorInfo
像上面那样强制转换,但我宁愿避免强制转换并MethodBase
直接使用该方法。我只是想不出正确的参数。
有什么帮助吗?非常感谢。
编辑
本杰明,
非常感谢您的建议。实际上,我正在按照您在第二次编辑中的建议进行操作,除了(这是一个很大的“例外”)我的字典在哪里
class ClonerMethod {
public MethodBase method;
public bool isConstructor;
...
public Object invoke(Object toClone) {
return isConstructor ?
((ConstructorInfo) method).Invoke(new object[] { toClone }) : //<-- I wanted to avoid this cast
method.Invoke(toClone, null);
}
}
And then I called ClonerMethod
's invoke
on what I found in the dictionary. I didn't add the code the deals with all that because the answer I was looking for was just how to call Invoke on a ConstructorInfo
using MethodBase
's Invoke
method, so I didn't want to add unnecessary info and too much code for you guys to read through. However, I like your use of Func<,>
much MUCH better, so I'm switching to that. Also making the Clone
method generic is a nice addition, but in my case the caller doesn't know the type of the object, so I'll keep it non-generic instead.
I didn't know about Func<,>
, and if I knew about the lambda operator I had forgotten (I hadn't really needed something like this before), so I've actually learnt a lot from your answer. I always love to learn new things, and this will come in very handy in the future, so thanks a lot! :)