如何修改附属于某个Type
的TypeAttributes
?
我想修改类型并通过应用TypeAttributes.Abstract
标志使其抽象,我该如何做到这一点?
我确信我需要调用到CLR的某个地方,但我无法追踪它从哪里得到这个信息,它似乎是一个无尽的系统的分层方法调用其他的
您可以使用诸如monol . cecil之类的库以编程方式更改类型。假设您在名为"ExistingAssembly.dll"的程序集中有一个名为"Test"的类,并且您希望将类"Test"转换为抽象类。现在需要做的就是:
void Main(){
//This is the existing assembly containing the type that you wish to modify
var assemblyFile = @"C:tempExistingAssembly.dll";
var ass = Mono.Cecil.AssemblyDefinition.ReadAssembly(assemblyFile);
var type = ass.MainModule.GetTypes().First(t => t.Name == "Test");
//Make the type an Abstract type (class)
type.IsAbstract = true;
//Finally save the modified assembly into a new file
ass.Write(@"C:tempModifiedAssembly.dll");
//The type "Test" in the above "ModifiedAssembly.dll" is now an abstract class.
}
// This is the Type that you wish to turn into an Abstract Class
public class Test {
public string DummyMethod(){
return "Dummy Return";
}
}
你可以得到单声道。塞西尔大会从这里(Nuget):https://www.nuget.org/packages/Mono.Cecil/