3

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;}
tomwesolowski
  • 956
  • 1
  • 11
  • 27

1 Answers1

6

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}");
Vagaus
  • 4,174
  • 20
  • 31