Ho il seguente albero di classe:C# GetMethod non restituisce un metodo genitore
public class A
{
public static object GetMe(SomeOtherClass something)
{
return something.Foo();
}
}
public class B:A
{
public static new object GetMe(SomeOtherClass something)
{
return something.Bar();
}
}
public class C:B
{
}
public class SomeOtherClass
{
}
Dato SomeOtherClass parameter = new SomeOtherClass()
) questo funziona:
typeof(B).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
Ma questo:
typeof(C).GetMethod("GetMe", new Type[] { typeof(SomeOtherClass) })).Invoke(null, parameter));
getta a NullReferenceException
, mentre vorrei che chiamasse lo stesso metodo esattamente come sopra.
Ho provato diverse bandiere di associazione inutilmente. Qualsiasi aiuto?
I metodi sono statici pubblici, ho storto la domanda (e ho pensato di avere la prova leggerlo D :). Grazie ancora! –
@ SebastiánVansteenkiste: In tal caso, la semplice modifica dei flag di bind per includere FlattenHierarchy (e statica e pubblica) dovrebbe andare bene. –
Mille grazie! Per le future generazioni: 'typeof (C) .GetMethod (" GetMe ", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static, null, new Tipo [] {typeof (SomeOtherClass)}, null)). Invoca (null , parametro)); ha fatto il trucco. –