コード例 #1
0
        public static AssemblyDefinition CreateAssembly(AssemblyNameDefinition assemblyName, string moduleName, ModuleParameters parameters)
        {
            if (assemblyName == null)
            {
                throw new ArgumentNullException("assemblyName");
            }
            if (moduleName == null)
            {
                throw new ArgumentNullException("moduleName");
            }
            Mixin.CheckParameters(parameters);
            if (parameters.Kind == ModuleKind.NetModule)
            {
                throw new ArgumentException("kind");
            }

            var assembly = ModuleDefinition.CreateModule(moduleName, parameters).Assembly;

            assembly.Name = assemblyName;

            return(assembly);
        }
コード例 #2
0
ファイル: Import.cs プロジェクト: vitek-karas/cecil
        public virtual AssemblyNameReference ImportReference(SR.AssemblyName name)
        {
            Mixin.CheckName(name);

            AssemblyNameReference reference;

            if (TryGetAssemblyNameReference(name, out reference))
            {
                return(reference);
            }

            reference = new AssemblyNameReference(name.Name, name.Version)
            {
                PublicKeyToken = name.GetPublicKeyToken(),
                Culture        = name.CultureInfo.Name,
                HashAlgorithm  = (AssemblyHashAlgorithm)name.HashAlgorithm,
            };

            module.AssemblyReferences.Add(reference);

            return(reference);
        }
コード例 #3
0
        public static ModuleDefinition CreateModule(string name, ModuleParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var module = new ModuleDefinition(GlobalAssemblyResolver.Instance)
            {
                Name            = name,
                kind            = parameters.Kind,
                Runtime         = parameters.Runtime,
                architecture    = parameters.Architecture,
                mvid            = Guid.NewGuid(),
                Attributes      = ModuleAttributes.ILOnly,
                Characteristics = (ModuleCharacteristics)0x8540,
            };

            if (parameters.AssemblyResolver != null)
            {
                module.assembly_resolver = parameters.AssemblyResolver;
            }

            if (parameters.MetadataResolver != null)
            {
                module.metadata_resolver = parameters.MetadataResolver;
            }

            if (parameters.Kind != ModuleKind.NetModule)
            {
                var assembly = new AssemblyDefinition();
                module.assembly      = assembly;
                module.assembly.Name = CreateAssemblyName(name);
                assembly.main_module = module;
            }

            module.Types.Add(new TypeDefinition(string.Empty, "<Module>", TypeAttributes.NotPublic));

            return(module);
        }
コード例 #4
0
 public virtual AssemblyNameReference ImportReference(AssemblyNameReference name)
 {
     Mixin.CheckName(name);
     if (module.TryGetAssemblyNameReference(name, out AssemblyNameReference assemblyNameReference))
     {
         return(assemblyNameReference);
     }
     assemblyNameReference = new AssemblyNameReference(name.Name, name.Version)
     {
         Culture          = name.Culture,
         HashAlgorithm    = name.HashAlgorithm,
         IsRetargetable   = name.IsRetargetable,
         IsWindowsRuntime = name.IsWindowsRuntime
     };
     byte[] array = (!name.PublicKeyToken.IsNullOrEmpty()) ? new byte[name.PublicKeyToken.Length] : Empty <byte> .Array;
     if (array.Length != 0)
     {
         Buffer.BlockCopy(name.PublicKeyToken, 0, array, 0, array.Length);
     }
     assemblyNameReference.PublicKeyToken = array;
     module.AssemblyReferences.Add(assemblyNameReference);
     return(assemblyNameReference);
 }
コード例 #5
0
        TypeDefinition GetNestedType(string fullname)
        {
            var names = fullname.Split('/');
            var type  = GetType(names[0]);

            if (type == null)
            {
                return(null);
            }

            for (int i = 1; i < names.Length; i++)
            {
                var nested_type = Mixin.GetNestedType(type, names[i]);
                if (nested_type == null)
                {
                    return(null);
                }

                type = nested_type;
            }

            return(type);
        }
コード例 #6
0
        static void AppendType(TypeReference type, StringBuilder name, bool fq_name, bool top_level)
        {
            var declaring_type = type.DeclaringType;

            if (declaring_type != null)
            {
                AppendType(declaring_type, name, false, top_level);
                name.Append('+');
            }

            var @namespace = type.Namespace;

            if (!string.IsNullOrEmpty(@namespace))
            {
                name.Append(@namespace);
                name.Append('.');
            }

            name.Append(type.GetElementType().Name);

            if (!fq_name)
            {
                return;
            }

            if (Mixin.IsTypeSpecification(type))
            {
                AppendTypeSpecification((TypeSpecification)type, name);
            }

            if (RequiresFullyQualifiedName(type, top_level))
            {
                name.Append(", ");
                name.Append(GetScopeFullName(type));
            }
        }
コード例 #7
0
        static bool AreSame(TypeReference a, TypeReference b)
        {
            if (ReferenceEquals(a, b))
            {
                return(true);
            }

            if (a == null || b == null)
            {
                return(false);
            }

            if (a.etype != b.etype)
            {
                return(false);
            }

            if (a.IsGenericParameter)
            {
                return(AreSame((GenericParameter)a, (GenericParameter)b));
            }

            if (Mixin.IsTypeSpecification(a))
            {
                return(AreSame((TypeSpecification)a, (TypeSpecification)b));
            }

            if (a.Name != b.Name || a.Namespace != b.Namespace)
            {
                return(false);
            }

            //TODO: check scope

            return(AreSame(a.DeclaringType, b.DeclaringType));
        }
コード例 #8
0
 public ArrayType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Array;
 }
コード例 #9
0
 public static bool GetHasGenericParameters(
     IGenericParameterProvider self,
     ModuleDefinition module)
 {
     return(Mixin.HasImage(module) && module.Read(self, (provider, reader) => reader.HasGenericParameters(provider)));
 }
コード例 #10
0
 public virtual MethodReference ImportReference(MethodReference method, IGenericParameterProvider context)
 {
     Mixin.CheckMethod(method);
     return(ImportMethod(method, ImportGenericContext.For(context)));
 }
コード例 #11
0
 internal PropertyReference(string name, TypeReference propertyType)
     : base(name)
 {
     Mixin.CheckType(propertyType, Mixin.Argument.propertyType);
     property_type = propertyType;
 }
コード例 #12
0
 public virtual TypeReference ImportReference(TypeReference type, IGenericParameterProvider context)
 {
     Mixin.CheckType(type);
     return(ImportType(type, ImportGenericContext.For(context)));
 }
コード例 #13
0
 public virtual FieldReference ImportReference(FieldReference field, IGenericParameterProvider context)
 {
     Mixin.CheckField(field);
     return(ImportField(field, ImportGenericContext.For(context)));
 }
コード例 #14
0
        static Stream GetFileStream(string fileName, FileMode mode, FileAccess access, FileShare share)
        {
            Mixin.CheckFileName(fileName);

            return(new FileStream(fileName, mode, access, share));
        }
コード例 #15
0
 public static bool GetHasSecurityDeclarations(
     ISecurityDeclarationProvider self,
     ModuleDefinition module)
 {
     return(Mixin.HasImage(module) && module.Read(self, (provider, reader) => reader.HasSecurityDeclarations(provider)));
 }
コード例 #16
0
 public PinnedType(TypeReference type) : base(type)
 {
     Mixin.CheckType(type);
     this.etype = Mono.Cecil.Metadata.ElementType.Pinned;
 }
コード例 #17
0
 public CustomAttributeNamedArgument(string name, CustomAttributeArgument argument)
 {
     Mixin.CheckName(name);
     this.name     = name;
     this.argument = argument;
 }
コード例 #18
0
 public SentinelType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Sentinel;
 }
コード例 #19
0
 public PointerType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Ptr;
 }
コード例 #20
0
        static Stream GetFileStream(string fileName, FileMode mode, FileAccess access, FileShare share)
        {
            Mixin.CheckFileName(fileName);

            return(new FileStream(uTinyRipper.FileUtils.ToLongPath(fileName), mode, access, share));
        }
コード例 #21
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);
            AssemblyDefinition assemblyDefinition = SearchDirectory(name, directories, parameters);

            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            if (name.IsRetargetable)
            {
                name = new AssemblyNameReference(name.Name, Mixin.ZeroVersion)
                {
                    PublicKeyToken = Empty <byte> .Array
                };
            }
            string directoryName = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            string[] array = on_mono ? new string[2]
            {
                directoryName,
                Path.Combine(directoryName, "Facades")
            } : new string[1]
            {
                directoryName
            };
            if (IsZero(name.Version))
            {
                assemblyDefinition = SearchDirectory(name, array, parameters);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            if (name.Name == "mscorlib")
            {
                assemblyDefinition = GetCorlib(name, parameters);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            assemblyDefinition = GetAssemblyInGac(name, parameters);
            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            assemblyDefinition = SearchDirectory(name, array, parameters);
            if (assemblyDefinition != null)
            {
                return(assemblyDefinition);
            }
            if (this.ResolveFailure != null)
            {
                assemblyDefinition = this.ResolveFailure(this, name);
                if (assemblyDefinition != null)
                {
                    return(assemblyDefinition);
                }
            }
            throw new AssemblyResolutionException(name);
        }
コード例 #22
0
 public PinnedType(TypeReference type) : base(type)
 {
     Mixin.CheckType(type);
     base.etype = ElementType.Pinned;
 }
コード例 #23
0
 public ByReferenceType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.ByRef;
 }
コード例 #24
0
 public PinnedType(TypeReference type)
     : base(type)
 {
     Mixin.CheckType(type);
     this.etype = MD.ElementType.Pinned;
 }
コード例 #25
0
 public CustomAttributeArgument(TypeReference type, object value)
 {
     Mixin.CheckType(type);
     this.type  = type;
     this.value = value;
 }
コード例 #26
0
 public RequiredModifierType(TypeReference modifierType, TypeReference type) : base(type)
 {
     Mixin.CheckModifier(modifierType, type);
     this.modifier_type = modifierType;
     base.etype         = ElementType.CModReqD;
 }
コード例 #27
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var assembly = SearchDirectory(name, directories, parameters);

            if (assembly != null)
            {
                return(assembly);
            }

            if (name.IsRetargetable)
            {
                // if the reference is retargetable, zero it
                name = new AssemblyNameReference(name.Name, Mixin.ZeroVersion)
                {
                    PublicKeyToken = Empty <byte> .Array,
                };
            }

#if NET_CORE
            assembly = SearchTrustedPlatformAssemblies(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#else
            var framework_dir  = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);
            var framework_dirs = on_mono
                                ? new [] { framework_dir, Path.Combine(framework_dir, "Facades") }
                                : new [] { framework_dir };

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, framework_dirs, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            if (name.Name == "mscorlib")
            {
                assembly = GetCorlib(name, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            assembly = GetAssemblyInGac(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }

            assembly = SearchDirectory(name, framework_dirs, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#endif
            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new AssemblyResolutionException(name);
        }
コード例 #28
0
        public ReflectionImporter(ModuleDefinition module)
        {
            Mixin.CheckModule(module);

            this.module = module;
        }
コード例 #29
0
        public TypeDefinition GetType(string @namespace, string name)
        {
            Mixin.CheckName(name);

            return(((TypeDefinitionCollection)this.Types).GetType(@namespace ?? string.Empty, name));
        }
コード例 #30
0
        public MetadataImporter(ModuleDefinition module)
        {
            Mixin.CheckModule(module);

            this.module = module;
        }