public void Emit(OpCode opcode, RuntimeMethodHandle meth, RuntimeTypeHandle typeContext) { if (meth.IsNullHandle()) { throw new ArgumentNullException("meth"); } if (typeContext.IsNullHandle()) { throw new ArgumentNullException("typeContext"); } // need to sort out the stack size story //int stackchange = 0; int tempVal = m_scope.GetTokenFor(meth, typeContext); EnsureCapacity(7); InternalEmit(opcode); // need to sort out the stack size story /* * if (opcode.m_push == StackBehaviour.Varpush * && meth.ReturnType != typeof(void)) { stackchange++; } * if (opcode.m_pop == StackBehaviour.Varpop) { * stackchange -= meth.GetParametersNoCopy().Length; } * if (!meth.IsStatic && !(opcode.Equals(OpCodes.Newobj))) {stackchange--; } * * UpdateStackSize(opcode, stackchange); */ // need to sort out the stack size story UpdateStackSize(opcode, 1); m_length = PutInteger4(tempVal, m_length, m_ILStream); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } #if !FEATURE_CORECLR if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage)) { FrameworkEventSource.Log.BeginGetMethodFromHandle(); } #endif MethodBase m = RuntimeType.GetMethodBase(handle.GetMethodInfo()); Type declaringType = m.DeclaringType; #if !FEATURE_CORECLR if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null) { FrameworkEventSource.Log.EndGetMethodFromHandle(declaringType.GetFullNameForEtw(), m.GetFullNameForEtw()); } #endif if (declaringType != null && declaringType.IsGenericType) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGeneric"), m, declaringType.GetGenericTypeDefinition())); } return(m); }
// this method is a big perf hit, so don't call unnecessarily internal static MethodInfo GetMethodInfo(RuntimeMethodHandle rmh) { if (rmh.IsNullHandle()) { return(null); } #if _DEBUG try { #endif // Assert here because reflection will check grants and if we fail the check, // there will be an infinite recursion that overflows the stack. PermissionSet.s_fullTrust.Assert(); RuntimeTypeHandle rth = rmh.GetDeclaringType(); return(System.RuntimeType.GetMethodBase(rth, rmh) as MethodInfo); #if _DEBUG } catch (Exception) { return(null); } #endif }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } #if !FEATURE_CORECLR && !MONO if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage)) { FrameworkEventSource.Log.BeginGetMethodFromHandle(); } #endif #if MONO MethodBase m = GetMethodFromHandleInternalType(handle.Value, declaringType.Value); if (m == null) { throw new ArgumentException("The handle is invalid."); } #else MethodBase m = RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo()); #endif #if !FEATURE_CORECLR && !MONO if (FrameworkEventSource.IsInitialized && FrameworkEventSource.Log.IsEnabled(EventLevel.Informational, FrameworkEventSource.Keywords.DynamicTypeUsage) && declaringType != null && m != null) { FrameworkEventSource.Log.EndGetMethodFromHandle(declaringType.GetRuntimeType().GetFullNameForEtw(), m.GetFullNameForEtw()); } #endif return(m); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } #if MONO MethodBase m = GetMethodFromHandleInternalType(handle.Value, IntPtr.Zero); if (m == null) { throw new ArgumentException("The handle is invalid."); } #else MethodBase m = RuntimeType.GetMethodBase(handle.GetMethodInfo()); #endif Type declaringType = m.DeclaringType; if (declaringType != null && declaringType.IsGenericType) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGeneric"), m, declaringType.GetGenericTypeDefinition())); } return(m); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo())); }
public static MethodBase?GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) { if (handle.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo())); }
internal static MethodBase InternalGetCurrentMethod(ref StackCrawlMark stackMark) { RuntimeMethodHandle currentMethod = RuntimeMethodHandle.GetCurrentMethod(ref stackMark); if (currentMethod.IsNullHandle()) { return(null); } return(RuntimeType.GetMethodBase(currentMethod.GetTypicalMethodDefinition())); }
public virtual MethodBase GetMethodBase(int i) { RuntimeMethodHandle handle = this.rgMethodHandle[i]; if (handle.IsNullHandle()) { return(null); } return(RuntimeType.GetMethodBase(handle.GetTypicalMethodDefinition())); }
public static void PrepareMethod(RuntimeMethodHandle method) { if (method.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } unsafe { PrepareMethod(method.Value, null, 0); } }
public void Emit(OpCode opcode, RuntimeMethodHandle meth) { if (meth.IsNullHandle()) { throw new ArgumentNullException("meth"); } int tokenFor = this.m_scope.GetTokenFor(meth); base.EnsureCapacity(7); base.InternalEmit(opcode); base.UpdateStackSize(opcode, 1); base.PutInteger4(tokenFor); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) { if (handle.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } MethodBase m = RuntimeMethodInfo.GetMethodFromHandleInternalType(handle.Value, declaringType.Value); if (m == null) { throw new ArgumentException(SR.Argument_InvalidHandle); } return(m); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } MethodBase methodBase = RuntimeType.GetMethodBase(handle); if ((methodBase.DeclaringType != null) && methodBase.DeclaringType.IsGenericType) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGeneric"), new object[] { methodBase, methodBase.DeclaringType.GetGenericTypeDefinition() })); } return(methodBase); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } MethodBase methodBase = RuntimeType.GetMethodBase(handle.GetMethodInfo()); Type declaringType = methodBase.DeclaringType; if (declaringType != (Type)null && declaringType.IsGenericType) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGeneric"), (object)methodBase, (object)declaringType.GetGenericTypeDefinition())); } return(methodBase); }
public static void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation) { if (method.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } unsafe { IntPtr[] instantiations = RuntimeTypeHandle.CopyRuntimeTypeHandles(instantiation, out int length); fixed(IntPtr *pinst = instantiations) { PrepareMethod(method.Value, pinst, length); GC.KeepAlive(instantiation); } } }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } #if MONO MethodBase m = GetMethodFromHandleInternalType(handle.Value, declaringType.Value); if (m == null) { throw new ArgumentException("The handle is invalid."); } return(m); #else return(RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo())); #endif }
public virtual MethodBase GetMethodBase(int i) { // There may be a better way to do this. // we got RuntimeMethodHandles here and we need to go to MethodBase // but we don't know whether the reflection info has been initialized // or not. So we call GetMethods and GetConstructors on the type // and then we fetch the proper MethodBase!! RuntimeMethodHandle mh = rgMethodHandle [i]; if (mh.IsNullHandle()) { return(null); } mh = mh.GetTypicalMethodDefinition(); return(RuntimeType.GetMethodBase(mh)); }
public static MethodBase?GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } MethodBase?m = RuntimeType.GetMethodBase(handle.GetMethodInfo()); Type?declaringType = m?.DeclaringType; if (declaringType != null && declaringType.IsGenericType) { throw new ArgumentException(SR.Format( SR.Argument_MethodDeclaringTypeGeneric, m, declaringType.GetGenericTypeDefinition())); } return(m); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(Environment.GetResourceString("Argument_InvalidHandle")); } MethodBase m = RuntimeType.GetMethodBase(handle.GetMethodInfo()); Type declaringType = m.DeclaringType; if (declaringType != null && declaringType.IsGenericType) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MethodDeclaringTypeGeneric"), m, declaringType.GetGenericTypeDefinition())); } return(m); }
public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) { if (handle.IsNullHandle()) { throw new ArgumentException(SR.Argument_InvalidHandle); } MethodBase m = RuntimeMethodInfo.GetMethodFromHandleInternalType(handle.Value, IntPtr.Zero); if (m == null) { throw new ArgumentException(SR.Argument_InvalidHandle); } Type declaringType = m.DeclaringType; if (declaringType != null && declaringType.IsGenericType) { throw new ArgumentException(string.Format(SR.Argument_MethodDeclaringTypeGeneric, m, declaringType.GetGenericTypeDefinition())); } return(m); }
internal static unsafe object[] GetCustomAttributes(Module decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType attributeFilterType, bool mustBeInheritable, IList derivedAttributes) { if (decoratedModule.Assembly.ReflectionOnly) { throw new InvalidOperationException(Environment.GetResourceString("Arg_ReflectionOnlyCA")); } MetadataImport metadataImport = decoratedModule.MetadataImport; CustomAttributeRecord[] customAttributeRecords = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); Type elementType = (((attributeFilterType == null) || attributeFilterType.IsValueType) || attributeFilterType.ContainsGenericParameters) ? typeof(object) : attributeFilterType; if ((attributeFilterType == null) && (customAttributeRecords.Length == 0)) { return(Array.CreateInstance(elementType, 0) as object[]); } object[] attributes = Array.CreateInstance(elementType, customAttributeRecords.Length) as object[]; int length = 0; SecurityContextFrame frame = new SecurityContextFrame(); frame.Push(decoratedModule.Assembly.InternalAssembly); Assembly lastAptcaOkAssembly = null; for (int i = 0; i < customAttributeRecords.Length; i++) { bool flag2; bool flag3; object obj2 = null; CustomAttributeRecord caRecord = customAttributeRecords[i]; RuntimeMethodHandle ctor = new RuntimeMethodHandle(); RuntimeType attributeType = null; int namedArgs = 0; IntPtr signature = caRecord.blob.Signature; IntPtr blobEnd = (IntPtr)(((void *)signature) + caRecord.blob.Length); if (FilterCustomAttributeRecord(caRecord, metadataImport, ref lastAptcaOkAssembly, decoratedModule, decoratedMetadataToken, attributeFilterType, mustBeInheritable, attributes, derivedAttributes, out attributeType, out ctor, out flag2, out flag3)) { if (!ctor.IsNullHandle()) { ctor.CheckLinktimeDemands(decoratedModule, decoratedMetadataToken); } RuntimeConstructorInfo.CheckCanCreateInstance(attributeType, flag3); if (flag2) { obj2 = CreateCaObject(decoratedModule, ctor, ref signature, blobEnd, out namedArgs); } else { obj2 = attributeType.TypeHandle.CreateCaInstance(ctor); if (Marshal.ReadInt16(signature) != 1) { throw new CustomAttributeFormatException(); } signature = (IntPtr)(((void *)signature) + 2); namedArgs = Marshal.ReadInt16(signature); signature = (IntPtr)(((void *)signature) + 2); } for (int j = 0; j < namedArgs; j++) { string str; bool flag4; Type type3; object obj3; IntPtr ptr1 = caRecord.blob.Signature; GetPropertyOrFieldData(decoratedModule, ref signature, blobEnd, out str, out flag4, out type3, out obj3); try { if (flag4) { if ((type3 == null) && (obj3 != null)) { type3 = (obj3.GetType() == typeof(RuntimeType)) ? typeof(Type) : obj3.GetType(); } RuntimePropertyInfo property = null; if (type3 == null) { property = attributeType.GetProperty(str) as RuntimePropertyInfo; } else { property = attributeType.GetProperty(str, type3, Type.EmptyTypes) as RuntimePropertyInfo; } RuntimeMethodInfo setMethod = property.GetSetMethod(true) as RuntimeMethodInfo; if (setMethod.IsPublic) { setMethod.MethodHandle.CheckLinktimeDemands(decoratedModule, decoratedMetadataToken); setMethod.Invoke(obj2, BindingFlags.Default, null, new object[] { obj3 }, null, true); } } else { (attributeType.GetField(str) as RtFieldInfo).InternalSetValue(obj2, obj3, BindingFlags.Default, Type.DefaultBinder, null, false); } } catch (Exception exception) { throw new CustomAttributeFormatException(string.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString(flag4 ? "RFLCT.InvalidPropFail" : "RFLCT.InvalidFieldFail"), new object[] { str }), exception); } } if (!signature.Equals(blobEnd)) { throw new CustomAttributeFormatException(); } attributes[length++] = obj2; } } frame.Pop(); if ((length == customAttributeRecords.Length) && (pcaCount == 0)) { return(attributes); } if (length == 0) { Array.CreateInstance(elementType, 0); } object[] destinationArray = Array.CreateInstance(elementType, (int)(length + pcaCount)) as object[]; Array.Copy(attributes, 0, destinationArray, 0, length); return(destinationArray); }
internal static unsafe bool FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, ref Assembly lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, bool mustBeInheritable, object[] attributes, IList derivedAttributes, out RuntimeType attributeType, out RuntimeMethodHandle ctor, out bool ctorHasParameters, out bool isVarArg) { ctor = new RuntimeMethodHandle(); attributeType = null; ctorHasParameters = false; isVarArg = false; IntPtr ptr1 = (IntPtr)(((void *)caRecord.blob.Signature) + caRecord.blob.Length); attributeType = decoratedModule.ResolveType(scope.GetParentToken((int)caRecord.tkCtor), null, null) as RuntimeType; if (!attributeFilterType.IsAssignableFrom(attributeType)) { return(false); } if (!AttributeUsageCheck(attributeType, mustBeInheritable, attributes, derivedAttributes)) { return(false); } if ((attributeType.Assembly != lastAptcaOkAssembly) && !attributeType.Assembly.AptcaCheck(decoratedModule.Assembly)) { return(false); } lastAptcaOkAssembly = decoratedModule.Assembly; ConstArray methodSignature = scope.GetMethodSignature(caRecord.tkCtor); isVarArg = (methodSignature[0] & 5) != 0; ctorHasParameters = methodSignature[1] != 0; if (ctorHasParameters) { ctor = decoratedModule.ModuleHandle.ResolveMethodHandle((int)caRecord.tkCtor); } else { ctor = attributeType.GetTypeHandleInternal().GetDefaultConstructor(); if (ctor.IsNullHandle() && !attributeType.IsValueType) { throw new MissingMethodException(".ctor"); } } if (ctor.IsNullHandle()) { if (!attributeType.IsVisible && !attributeType.TypeHandle.IsVisibleFromModule(decoratedModule.ModuleHandle)) { return(false); } return(true); } if (ctor.IsVisibleFromModule(decoratedModule)) { return(true); } MetadataToken token = new MetadataToken(); if (decoratedToken.IsParamDef) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); token = new MetadataToken(scope.GetParentToken((int)token)); } else if ((decoratedToken.IsMethodDef || decoratedToken.IsProperty) || (decoratedToken.IsEvent || decoratedToken.IsFieldDef)) { token = new MetadataToken(scope.GetParentToken((int)decoratedToken)); } else if (decoratedToken.IsTypeDef) { token = decoratedToken; } return(token.IsTypeDef && ctor.IsVisibleFromType(decoratedModule.ModuleHandle.ResolveTypeHandle((int)token))); }