6
I am trying to create a method that takes a string and returns an instance of the class with the name passed, example: "Calculator", will return me an instance of the class "Calculator" (If it exists), I have a problem at the time of setting which type it returns, 'Cause from what I’ve researched, I need to get the guy to the method, too. I tried to: Type Tipo = Type.GetType("NomeDaClasse");
and then passing var x = RetornaClasse<Tipo>("Calculadora");
but without success. (I can’t even set the method to try to use.
static T RetornaClasse<T>(string nome) where T : class
{
return (T)Assembly.GetExecutingAssembly().CreateInstance(nome);
}
Use: the return of this method will be parameter for this Generic Class:
public class Generico<T> where T : class
{
public T Objeto { get; set; }
public string ListarAtributos()
{
var p = Objeto.GetType().GetProperties();
string atributos = String.Empty;
foreach (PropertyInfo pop in p) // Lista de atributos
atributos += (String.Concat("[", pop.Name, "] = [", pop.GetValue(Objeto, null), "]\n"));
return atributos;
}
public string[] ListarMetodos()
{
var m = Objeto.GetType().GetMethods();
string[] metodos = new string[m.Length];
for (int i = 0; i < m.Length; i++)
metodos[i] = String.Concat(i, " [+] ", m[i].Name, "\n");
return metodos;
}
// TODO: Setar atributos, usar métodos
}
Solution:
static object RetornaClasse(string nome)
{
var classe = Assembly.GetExecutingAssembly().GetTypes().First(x => x.Name == nome);
var instancia = Activator.CreateInstance(classe);
return instancia;
}
The guy’s in the same Assembly, or a third party?
– Miguel Angelo
In the same Assembly.
– Laerte
You would be able to edit the question, indicating how you intend to use the return method?
– Miguel Angelo
If the methods and properties will be accessed via reflection, then there is no need for the generic parameter.
– Miguel Angelo
I think the confusion is on that line
T RetornaClasse<T>(string nome) where T : class
... this does not return a class by itself, but rather an instance of a T object, in which T can only be a class and not a struct. That is, when calling the methodRetornaClasse
only classes can be specified as generic parameter, and not structs:RetornaClasse<string>()
is valid becausestring
is class,RetornaClasse<int>
is not valid becauseint
is struct.– Miguel Angelo
I took your advice and took the <T> parameter from the Generic class and changed the attribute to Object, I left the method returning an Object, only now it instantiates but I can’t use the empty constructor of the "Calculator" class, as I prompt it with the empty constructor (I came to reset the parameters), EDIT: I managed!
– Laerte
It’s using the method
Activator.CreateInstance(tipo)
.– Miguel Angelo
That’s what I did! Thank you Miguel! :)
– Laerte
I edited the answer with an example of how to use an object using reflection, and using
dynamic
... I think it will be in your interest to check the use of Dynamic in this case, as it makes it much easier.– Miguel Angelo