本文整理匯總了C#中System.RuntimeFieldHandle.GetApproxDeclaringType方法的典型用法代碼示例。如果您正苦於以下問題:C# RuntimeFieldHandle.GetApproxDeclaringType方法的具體用法?C# RuntimeFieldHandle.GetApproxDeclaringType怎麽用?C# RuntimeFieldHandle.GetApproxDeclaringType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.RuntimeFieldHandle
的用法示例。
在下文中一共展示了RuntimeFieldHandle.GetApproxDeclaringType方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ResolveField
public FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
{
MetadataToken tk = new MetadataToken(metadataToken);
if (!MetadataImport.IsValidToken(tk))
throw new ArgumentOutOfRangeException("metadataToken",
String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", tk, this)));
RuntimeTypeHandle[] typeArgs = ConvertToTypeHandleArray(genericTypeArguments);
RuntimeTypeHandle[] methodArgs = ConvertToTypeHandleArray(genericMethodArguments);
try
{
RuntimeFieldHandle fieldHandle = new RuntimeFieldHandle();
if (!tk.IsFieldDef)
{
if (!tk.IsMemberRef)
throw new ArgumentException("metadataToken",
String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), tk, this));
unsafe
{
ConstArray sig = MetadataImport.GetMemberRefProps(tk);
if (*(CorCallingConvention*)sig.Signature.ToPointer() != CorCallingConvention.Field)
throw new ArgumentException("metadataToken",
String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), tk, this));
}
fieldHandle = GetModuleHandle().ResolveFieldHandle(tk, typeArgs, methodArgs);
}
fieldHandle = GetModuleHandle().ResolveFieldHandle(metadataToken, typeArgs, methodArgs);
Type declaringType = fieldHandle.GetApproxDeclaringType().GetRuntimeType();
if (declaringType.IsGenericType || declaringType.IsArray)
{
int tkDeclaringType = GetModuleHandle().GetMetadataImport().GetParentToken(metadataToken);
declaringType = ResolveType(tkDeclaringType, genericTypeArguments, genericMethodArguments);
}
return System.RuntimeType.GetFieldInfo(declaringType.GetTypeHandleInternal(), fieldHandle);
}
catch(MissingFieldException)
{
return ResolveLiteralField(tk, genericTypeArguments, genericMethodArguments);
}
catch (BadImageFormatException e)
{
throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), e);
}
}
示例2: ResolveField
public unsafe FieldInfo ResolveField(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
{
FieldInfo fieldInfo;
System.Reflection.MetadataToken token = new System.Reflection.MetadataToken(metadataToken);
if (!this.MetadataImport.IsValidToken((int) token))
{
throw new ArgumentOutOfRangeException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidToken", new object[] { token, this }), new object[0]));
}
RuntimeTypeHandle[] typeInstantiationContext = ConvertToTypeHandleArray(genericTypeArguments);
RuntimeTypeHandle[] methodInstantiationContext = ConvertToTypeHandleArray(genericMethodArguments);
try
{
RuntimeFieldHandle fieldHandle = new RuntimeFieldHandle();
if (!token.IsFieldDef)
{
if (!token.IsMemberRef)
{
throw new ArgumentException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), new object[] { token, this }));
}
if (*(((byte*) this.MetadataImport.GetMemberRefProps((int) token).Signature.ToPointer())) != 6)
{
throw new ArgumentException("metadataToken", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_ResolveField"), new object[] { token, this }));
}
fieldHandle = this.GetModuleHandle().ResolveFieldHandle((int) token, typeInstantiationContext, methodInstantiationContext);
}
fieldHandle = this.GetModuleHandle().ResolveFieldHandle(metadataToken, typeInstantiationContext, methodInstantiationContext);
Type runtimeType = fieldHandle.GetApproxDeclaringType().GetRuntimeType();
if (runtimeType.IsGenericType || runtimeType.IsArray)
{
int parentToken = this.GetModuleHandle().GetMetadataImport().GetParentToken(metadataToken);
runtimeType = this.ResolveType(parentToken, genericTypeArguments, genericMethodArguments);
}
fieldInfo = System.RuntimeType.GetFieldInfo(runtimeType.GetTypeHandleInternal(), fieldHandle);
}
catch (MissingFieldException)
{
fieldInfo = this.ResolveLiteralField((int) token, genericTypeArguments, genericMethodArguments);
}
catch (BadImageFormatException exception)
{
throw new ArgumentException(Environment.GetResourceString("Argument_BadImageFormatExceptionResolve"), exception);
}
return fieldInfo;
}