public static CompilerMetadataReference? Create(IRawModuleBytesProvider rawModuleBytesProvider, ModuleDef module, bool makeEverythingPublic) {
			var moduleData = rawModuleBytesProvider.GetRawModuleBytes(module);
			if (moduleData == null)
				return null;
			if (makeEverythingPublic) {
				bool success = new MetadataFixer(moduleData).MakePublic();
				if (!success)
					return null;
			}
			var asmRef = module.Assembly.ToAssemblyRef();
			if (module.IsManifestModule)
				return CompilerMetadataReference.CreateAssemblyReference(moduleData, asmRef, module.Location);
			return CompilerMetadataReference.CreateModuleReference(moduleData, asmRef, module.Location);
		}
Ejemplo n.º 2
0
        public bool Patch(ModuleDef module, out byte[] newModuleData)
        {
            var moduleData = this.moduleData;

            // NOTE: We can't remove the type from the corlib (eg. mscorlib) because the compiler
            // (Roslyn) won't recognize it as the corlib if it has any AssemblyRefs.
            // A possible fix is to add a new netmodule to the corlib assembly.
            bool fixTypeDefRefs = nonNestedEditedTypeOrNull != null &&
                                  MDPatcherUtils.ExistsInMetadata(nonNestedEditedTypeOrNull) &&
                                  MDPatcherUtils.ReferencesModule(module, nonNestedEditedTypeOrNull?.Module) &&
                                  !module.Assembly.IsCorLib();

            if (makeEverythingPublic || fixTypeDefRefs)
            {
                using (var md = MDPatcherUtils.TryCreateMetadata(moduleData)) {
                    if (makeEverythingPublic)
                    {
                        bool success = new MetadataFixer(moduleData, md).MakePublic();
                        if (!success)
                        {
                            newModuleData = null;
                            return(false);
                        }
                    }
                    if (fixTypeDefRefs)
                    {
                        var mdEditor = new MetadataEditor(moduleData, md);
                        var patcher  = new MDEditorPatcher(moduleData, mdEditor, tempAssembly, nonNestedEditedTypeOrNull);
                        patcher.Patch(module);
                        if (mdEditor.MustRewriteMetadata())
                        {
                            var stream = new MDWriterMemoryStream();
                            new MDWriter(moduleData, mdEditor, stream).Write();
                            moduleData = stream.ToArray();
                        }
                    }
                }
            }

            newModuleData = moduleData;
            return(true);
        }
Ejemplo n.º 3
0
        public static CompilerMetadataReference?Create(IRawModuleBytesProvider rawModuleBytesProvider, ModuleDef module, bool makeEverythingPublic)
        {
            var moduleData = rawModuleBytesProvider.GetRawModuleBytes(module);

            if (moduleData == null)
            {
                return(null);
            }
            if (makeEverythingPublic)
            {
                bool success = new MetadataFixer(moduleData).MakePublic();
                if (!success)
                {
                    return(null);
                }
            }
            var asmRef = module.Assembly.ToAssemblyRef();

            if (module.IsManifestModule)
            {
                return(CompilerMetadataReference.CreateAssemblyReference(moduleData, asmRef, module.Location));
            }
            return(CompilerMetadataReference.CreateModuleReference(moduleData, asmRef, module.Location));
        }