What version of .NET containing RuntimeMethodHandle.EmptyHandle?
No one answers my question in Difference between Activator.CreateInstance() and typeof(T).InvokeMember() with BindingFlags.CreateInstance. I guess that the issue is closed and I am asking here again.
Ben M have a nice function which I need for a project. The function is -
public static T CreateInstance<T>()
{
bool bNeedSecurityCheck = true;
bool canBeCached = false;
RuntimeMethodHandle emptyHandle = RuntimeMethodHandle.EmptyHandle;
return (T) RuntimeTypeHandle.CreateInstance(typeof(T) as RuntimeType, true, true, ref canBeCached, ref emptyHandle, ref bNeedSec开发者_运维知识库urityCheck);
}
I am using .NET 3.5 and Visual studio 2008. It gives the error "EmptyHandle type name does not exist in the type System.RuntimeMethodHandle".
Then I used "RuntimeMethodHandle emptyHandle = new RuntimeMethodHandle();". Another error presents. RuntimeTypeHandle does not have a CreateInstance method either. Activator has. But it does not take any ref parameter.
Thanks for clarification in advance!
EmptyHandle
is an internal static property on RuntimeMethodHandle
. From Reflector:
internal static RuntimeMethodHandle EmptyHandle {
get {
return new RuntimeMethodHandle(null);
}
}
Luckily, you don't have to implement the method yourself - use Activator.CreateInstance<T>
(Ben M's 'nice function' was just the decompiled source code for that .NET class method).
I see no indication that it exists even in .NET 4 - at least publicly.
I suspect it may be an internal property, but if that's the case you should treat it as if it didn't exist.
精彩评论