8
I have a method that lists the methods that a given object has, I want to take only the methods created by the user, in this case: Add and Subtract.
What this method returns to me:
Generic Class
public class Generico
{
public object Objeto { get; set; }
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;
}
}
Class Example
public class Calculadora
{
public int x { get; set; }
public int y { get; set; }
public Calculadora() { }
public int Somar()
{
return this.x + this.y;
}
public int Subtrair()
{
return this.x - this.y;
}
}
I tested here Miguel, did not return any method.
– Laerte
How strange... here works normally.
– Miguel Angelo
@Miguelangelo Using LINQ, how about expanding the
Where()
to ensure the safety of the return?– Leonel Sanches da Silva
What do you mean? You mean I make one
foreach
instead of using LINQ?– Miguel Angelo
@mgibsonbr I took the liberty of also putting the filter to remove getters/setters and other special methods.
– Miguel Angelo
I was gonna say
Where(mi => mi.DeclaringType == t && !mi.IsSpecialName)
, but they were faster :P– Leonel Sanches da Silva
@I think I got it... I edited it... see if that’s what you were talking about.
– Miguel Angelo
Here’s a push... congratulations on the 10k !
– Sergio
Thanks... That digit was hard, now I just want to see the next one. Maybe I’ll be alive by then! = D
– Miguel Angelo