示例#1
0
        // i hate you, ldtoken
        public static void SerializeMetadataTokenProvider(IMetadataTokenProvider tokenProvider, BinaryWriter writer)
        {
            Type type = tokenProvider.GetType();

            if (type == typeof(TypeReference))
            {
                writer.Write((byte)0);
                SerializeTypeReference(tokenProvider as TypeReference, writer);
            }
            else if (type == typeof(MethodReference))
            {
                writer.Write((byte)1);
                SerializeMethodReference(tokenProvider as MethodReference, writer);
            }
            else if (type == typeof(FieldReference))
            {
                writer.Write((byte)2);
                SerializeFieldReference(tokenProvider as FieldReference, writer);
            }
            else if (type == typeof(TypeDefinition))
            {
                writer.Write((byte)3);
                SerializeTypeDefinition(tokenProvider as TypeDefinition, writer);
            }
            else if (type == typeof(MethodDefinition))
            {
                writer.Write((byte)4);
                SerializeMethodDefinition(tokenProvider as MethodDefinition, writer);
            }
            else if (type == typeof(FieldDefinition))
            {
                writer.Write((byte)5);
                SerializeFieldDefinition(tokenProvider as FieldDefinition, writer);
            }
        }
示例#2
0
        void LogValidationWarning(IMetadataTokenProvider provider, IMetadataTokenProvider baseProvider, IMemberDefinition origin)
        {
            Debug.Assert(provider.GetType() == baseProvider.GetType());
            Debug.Assert(!(provider is GenericParameter genericParameter) || genericParameter.DeclaringMethod != null);
            switch (provider)
            {
            case ParameterDefinition parameterDefinition:
                var baseParameterDefinition = (ParameterDefinition)baseProvider;
                _context.LogWarning(
                    $"'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the parameter '{DiagnosticUtilities.GetParameterNameForErrorMessage (parameterDefinition)}' of method '{DiagnosticUtilities.GetMethodSignatureDisplayName (parameterDefinition.Method)}' " +
                    $"don't match overridden parameter '{DiagnosticUtilities.GetParameterNameForErrorMessage (baseParameterDefinition)}' of method '{DiagnosticUtilities.GetMethodSignatureDisplayName (baseParameterDefinition.Method)}'. " +
                    $"All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.",
                    2092, origin, subcategory: MessageSubCategory.TrimAnalysis);
                break;

            case MethodReturnType methodReturnType:
                _context.LogWarning(
                    $"'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the return value of method '{DiagnosticUtilities.GetMethodSignatureDisplayName (methodReturnType.Method)}' " +
                    $"don't match overridden return value of method '{DiagnosticUtilities.GetMethodSignatureDisplayName (((MethodReturnType) baseProvider).Method)}'. " +
                    $"All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.",
                    2093, origin, subcategory: MessageSubCategory.TrimAnalysis);
                break;

            // No fields - it's not possible to have a virtual field and override it
            case MethodDefinition methodDefinition:
                _context.LogWarning(
                    $"'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the implicit 'this' parameter of method '{DiagnosticUtilities.GetMethodSignatureDisplayName (methodDefinition)}' " +
                    $"don't match overridden implicit 'this' parameter of method '{DiagnosticUtilities.GetMethodSignatureDisplayName ((MethodDefinition) baseProvider)}'. " +
                    $"All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.",
                    2094, origin, subcategory: MessageSubCategory.TrimAnalysis);
                break;

            case GenericParameter genericParameterOverride:
                var genericParameterBase = (GenericParameter)baseProvider;
                _context.LogWarning(
                    $"'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on the generic parameter '{genericParameterOverride.Name}' of '{DiagnosticUtilities.GetGenericParameterDeclaringMemberDisplayName (genericParameterOverride)}' " +
                    $"don't match overridden generic parameter '{genericParameterBase.Name}' of '{DiagnosticUtilities.GetGenericParameterDeclaringMemberDisplayName (genericParameterBase)}'. " +
                    $"All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.",
                    2095, origin, subcategory: MessageSubCategory.TrimAnalysis);
                break;

            default:
                throw new NotImplementedException($"Unsupported provider type{provider.GetType ()}");
            }
        }
示例#3
0
        public static IMetadataTokenProvider Relink(this IMetadataTokenProvider mtp, Relinker relinker,
                                                    IGenericParameterProvider context)
        {
            switch (mtp)
            {
            case TypeReference reference:
                return(reference.Relink(relinker, context));

            case MethodReference reference:
                return(reference.Relink(relinker, context));

            case FieldReference reference:
                return(reference.Relink(relinker, context));

            case ParameterDefinition definition:
                return(definition.Relink(relinker, context));

            case CallSite site:
                return(site.Relink(relinker, context));

            default:
                throw new InvalidOperationException(
                          $"MonoMod can't handle metadata token providers of the type {mtp.GetType()}");
            }
        }
示例#4
0
 public static IMetadataTokenProvider Relink(this IMetadataTokenProvider mtp, Relinker relinker, IGenericParameterProvider context)
 {
     if (mtp is TypeReference)
     {
         return(((TypeReference)mtp).Relink(relinker, context));
     }
     if (mtp is MethodReference)
     {
         return(((MethodReference)mtp).Relink(relinker, context));
     }
     if (mtp is FieldReference)
     {
         return(((FieldReference)mtp).Relink(relinker, context));
     }
     if (mtp is ParameterDefinition)
     {
         return(((ParameterDefinition)mtp).Relink(relinker, context));
     }
     throw new InvalidOperationException($"MonoMod can't handle metadata token providers of the type {mtp.GetType()}");
 }
 public static void SetPublic(this IMetadataTokenProvider mtp, bool p)
 {
     if (mtp is TypeDefinition)
     {
         ((TypeDefinition)mtp).SetPublic(p);
     }
     else if (mtp is FieldDefinition)
     {
         ((FieldDefinition)mtp).SetPublic(p);
     }
     else if (mtp is MethodDefinition)
     {
         ((MethodDefinition)mtp).SetPublic(p);
     }
     else if (mtp is PropertyDefinition)
     {
         ((PropertyDefinition)mtp).SetPublic(p);
     }
     else if (mtp is EventDefinition)
     {
         ((EventDefinition)mtp).SetPublic(p);
     }
     else
     {
         throw new InvalidOperationException($"MonoMod can't set metadata token providers of the type {mtp.GetType()} public.");
     }
 }