How to pass a Class as parameter for a method?
Are you looking for type parameters?
Example:
public void ClassGet<T>(string blabla) where T : new()
{
var myClass = new T();
//Do something with blablah
}
You could send it as a parameter of the type Type
, but then you would need to use reflection to create an instance of it. You can use a generic parameter instead:
public void ClassGet<MyClassName>(string blabla) where MyClassName : new() {
MyClassName NewInstance = new MyClassName();
}
The function you're trying to implement already exists (a bit different)
Look at the Activator class: http://msdn.microsoft.com/en-us/library/system.activator.aspx
example:
private static object CreateByTypeName(string typeName)
{
// scan for the class type
var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from t in assembly.GetTypes()
where t.Name == typeName // you could use the t.FullName as well
select t).FirstOrDefault();
if (type == null)
throw new InvalidOperationException("Type not found");
return Activator.CreateInstance(type);
}
Usage:
var myClassInstance = CreateByTypeName("MyClass");