lundi 13 février 2017

How to convert a reference type to its "non reference" equivalent?

Suppose I have the following:

var t = typeof(Foo).MakeByRefType();

are there any ways to convert the result back to typeof(Foo)?

Honestly, the only way I've found is this:

var t = typeof(Foo).MakeByRefType();
string name = t.Name.Trim('&');
string asmQualified = t.AssemblyQualifiedName.Replace(t.Name, name);
var v = Type.GetType(asmQualified);

Other methods I've tried doesn't seems to work:

  1. Are there better ways to do this?
  2. If this is the only way, are there situations in which it can fail (maybe with dynamically generated assemblies)?




Aucun commentaire:

Enregistrer un commentaire