mercredi 28 septembre 2016

How to get type of custom class from string c#

I am trying to use GetType method for my custom class . I have the name of the class as string and i want to get type of it dynamically.

For Example:

MyClass.cs:

namespace ConsoleApplication1.Folder1
{
    public class MyClass
    {
        public void PrintMe()
        {
            System.Console.WriteLine("I am Folder 1 Class");
        }
    }
}

Namespace is ConsoleApplication1

MyClass is in the Folder1.

I want to get it's type from such a string:

var runtimeString = "MyClass" 

There is method mentioned in MSDN named GetType(string fileName)

How can i get type of the file and resolve it from the serviceLocator with type on runtime like:

var typeOfMyClass = GetType(runtimeString);    
var instanceOfMyClass = ServiceLocator.Resolve<TypeOfMyClass>(); 





Aucun commentaire:

Enregistrer un commentaire