tomwesolowski
tomwesolowski

Reputation: 956

Mono.Cecil - Getting TypeReference from System.Type

Is there possible to get TypeReference or TypeDefinition of type assigned to System.Type variable?

To be more concrete, I'm trying to get String type from following definition of attribute:

Custom(Value=typeof(String))]
        public string SomeProperty {get; set;}

Upvotes: 3

Views: 2323

Answers (1)

Vagaus
Vagaus

Reputation: 4215

You can use ModuleDefinition.ImportReference():

var a = AssemblyDefinition.ReadAssembly(typeof(Program).Assembly.Location);
var type = typeof(string);
var tr = a.MainModule.ImportReference(type);
var td = tr.Resolve();
Console.WriteLine($"tr = {tr}\ntd = {td}");

Upvotes: 7

Related Questions