1

I want to get the type of the value stored in the 'result' variable. Assume I dont know the 'result' value and the value comes during runtime.

string result="System.IO.Directory";

MetadataReference[] references =
{
    MetadataReference.CreateFromFile(path: typeof(result).Assembly.Location)
}

Is there any way to do this?

Emily Wong
  • 297
  • 3
  • 10
  • See if this question helps: https://stackoverflow.com/questions/8012724/how-can-i-get-a-type-from-an-assembly-that-is-loaded-from-within-another-folder – default locale Dec 08 '19 at 10:40

1 Answers1

1

Would the following statement work for your? The string below should be in a format like this "FullyQualifiedName, AssembleName"

Type type = Type.GetType("System.IO.Directory, System.IO.FileSystem");
MetadataReference[] references =
{
    MetadataReference.CreateFromFile(path: 
     type.Assembly.Location)
 }

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

Word to the wise, these solutions always require some type of reflection, and while it is a tool as good as any, too much of it can cause performance issues.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Does this code really works? I get syntax errors, doing on .net 4.7.1 – Emily Wong Dec 07 '19 at 08:21
  • Changed to Type type = Type.GetType("System.IO.Direcory"); and nore more errors. but it cant get type of the references that are not loaded. are there anyway to solve this? – Emily Wong Dec 07 '19 at 08:26
  • e.g. the following returns null Type type = Type.GetType("System.Drawing.Rectangle"); – Emily Wong Dec 07 '19 at 08:28