Getting type variable knowing C # type name

I am developing a class TypeTranslatorthat has a method Type TranslateType(Type type). This method gets the type of interface, and if there is a class of the name of the interface without a leading I, it creates it, otherwise an exception is thrown.
Here is some code to clear what was written earlier:

class Program
{
    interface IAnimal { }
    class Animal : IAnimal { }

void Function()
{
    TypeTranslator typeTranslator = new TypeTranslator();
    Assert(typeTranslator.TranslateType(typeof(IAnimal) == typeof(Animal)));
}

}

code>

Is it possible to get what I want?
Thanks for the help!

+3
source share
4 answers

Take a look Type.GetType. The documentation can be found here .

+3
source
+2
+2

You can see Object.GetType(), Assembly.GetTypes()(eg using Assembly.GetExecutingAssembly()), Type.IsClass, Type.IsInterface, Type.Name(which is similar to FullName, but without full qualifier) and the class Activatorto instantiate. It basically gets all types, iterates over them, checks if they are class types and if their name is one of the interfaces.

0
source

Source: https://habr.com/ru/post/1749372/


All Articles