private static void ProcessAssemblyTypes( AssemblyDefinition assembly, MonoCecilUtility utility, ModificationOptions options) { List<TypeDefinition> typesToRemove = new List<TypeDefinition>(); foreach (TypeDefinition currentType in assembly.MainModule.Types) { bool isServiceContract = utility.HasAttribute<ServiceContractAttribute>(currentType); bool isDataContract = utility.HasAttribute<DataContractAttribute>(currentType); if (isServiceContract) { currentType.Name = currentType.Name.Replace("Wcf", string.Empty); ChangeNamespace(currentType, options.ServiceContractsNamespace); } else if (isDataContract) { ChangeNamespace(currentType, options.ModelsNamespace); } else if (currentType.Name == "<Module>") { Log($"Leaving special type {currentType.FullName}"); } else { typesToRemove.Add(currentType); Log($"Removed {currentType.FullName}"); } } foreach (TypeDefinition typeToRemove in typesToRemove) { assembly.MainModule.Types.Remove(typeToRemove); } }
private static void ModifyAssemblyTitleAttribute( AssemblyDefinition assembly, MonoCecilUtility utility, ModificationOptions options) { CustomAttribute assemblyTitle = utility.GetFirstAttribute<AssemblyTitleAttribute>(assembly); // assembly title attribute has just 1 argument CustomAttributeArgument currentConstructurArgument = assemblyTitle.ConstructorArguments[0]; assemblyTitle.ConstructorArguments[0] = new CustomAttributeArgument( currentConstructurArgument.Type, options.TargetAssemblyName); Log($"Changed assembly title attribute from {currentConstructurArgument.Value} to {options.TargetAssemblyName}"); }
/// <summary> /// We can use the console arguments. Hardcoded data as this is just a proof of concept. /// </summary> public static void Main(string[] args) { ModificationOptions options = new CommandLineParser().ParseCommandLine(args); AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(options.SourceAssemblyFile); MonoCecilUtility utility = new MonoCecilUtility(); ModifyAssemblyName(assembly, options); ModifyAssemblyTitleAttribute(assembly, utility, options); ProcessAssemblyTypes(assembly, utility, options); assembly.Write(options.TargetAssemblyFile); }