3

I'm developing a TypeTranslator class which has a method Type TranslateType(Type type). This method gets a type of an interface and if there's a class of interface name without leading I it creates it, otherwise an exception is raised.
Here's some code to clearify what's written before:

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

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

}

Is it possible to get what I want?
Thank you for your help!

asked Jun 10, 2010 at 10:28

4 Answers 4

3

Look at Type.GetType. The documentation can be found here.

answered Jun 10, 2010 at 10:30
2

Do you want to get a type name from a string variable? If yes, then you can use Type.GetType method. Of course, the method expects the type name to be qualified by its namespace. Alternatively, if you don't want to restrict the type to a specific namespace, you can grab all types in an assembly with Assembly.GetTypes method and look for a type with a specific name.

answered Jun 10, 2010 at 10:31
2

Have you heard about dependeny injection? Maybe this is exactly what you need ...

See what is dependency injection and maybe on wikipedia for more infos, too.

answered Jun 10, 2010 at 10:33
0

You may want to have a look at Object.GetType(), Assembly.GetTypes() (using Assembly.GetExecutingAssembly(), for example) , Type.IsClass, Type.IsInterface, Type.Name (which is like FullName but without the full qualifier) and the Activator class to create an instance. It's basically getting all types, looping over them, checking whether they are class types and whether their name is the one of the interface.

answered Jun 10, 2010 at 10:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.