class A
{
....
}
A myA = new A();
C# reflection
Код:
Как добраться к имени объекта, то есть как получить строку "myA" ??
Прочитал несколько статей на эту тему, как работать со свойствами и методами вроде понятно, а вот как здесь быть...
Код:
class A
{
....
}
A myA = new A();
A myAcopy = myA;
{
....
}
A myA = new A();
A myAcopy = myA;
Какую строчку теперь ты хочеш получить ? :)
Цитата: burst
Как с помощью механизма отражения получить имя объекта
Никак. Имена локальных переменных существуют только для программиста, для компилятора же - это смещения на стеке.
Тогда другой вопрос. Такая ситуация.
Код:
class A
{
private int p1;
public int P1
{
get{return p1;}
set{p1 = value;}
}
public void method()
{
}
}
class B:A
{
private A a;
private int p2;
/*...get, set...*/
}
{
private int p1;
public int P1
{
get{return p1;}
set{p1 = value;}
}
public void method()
{
}
}
class B:A
{
private A a;
private int p2;
/*...get, set...*/
}
Мне нужно разбирать каждое свойство объекта класса В, и если оно - класс, производный от A, то вызывать определенный метод, который наследуется от А
Я думаю так:
Код:
B b = new B();
Type type = b.GetType();
PropertyInfo [] properties = type.GetProperties();
foreach(PropertyInfo property in properties)
{
if (isExtendorofA) //определяем что данное свойство - класс наследник А
{
//как вызвать method()?
//если делать Type nt = property.GetType();
//Method Info mi = nt.GetMethod("method"); - mi остается null
//mi.Invoke(property,null) - не вызывается.
}
}
Type type = b.GetType();
PropertyInfo [] properties = type.GetProperties();
foreach(PropertyInfo property in properties)
{
if (isExtendorofA) //определяем что данное свойство - класс наследник А
{
//как вызвать method()?
//если делать Type nt = property.GetType();
//Method Info mi = nt.GetMethod("method"); - mi остается null
//mi.Invoke(property,null) - не вызывается.
}
}
Надеюсь понятно изложил...
Код:
B b = new B();
Type type = b.GetType();
Type typeA = typeof(A);
foreach(PropertyInfo property in type.GetProperties())
{
Type propType = property.PropertyType;
if (propType.IsSubclassOf(typeA) || propType.Equals(typeA)) //определяем что данное свойство - класс наследник А
{
A propValue = (A) property.GetValue(b, null);
propValue.method();
}
}
Type type = b.GetType();
Type typeA = typeof(A);
foreach(PropertyInfo property in type.GetProperties())
{
Type propType = property.PropertyType;
if (propType.IsSubclassOf(typeA) || propType.Equals(typeA)) //определяем что данное свойство - класс наследник А
{
A propValue = (A) property.GetValue(b, null);
propValue.method();
}
}
Код:
[SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Method([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] Message)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#2b91af] Console[/COLOR][/SIZE][SIZE=2].Out.WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Call to A.Method(\"{0}\")"[/COLOR][/SIZE][SIZE=2], Message ?? [/SIZE][SIZE=2][COLOR=#a31515]"<N/A>"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]B[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] private [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2] member1 = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] [SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE]();[/SIZE]
[SIZE=2][COLOR=#0000ff] private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] member2 = 0;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2] Member1 { [/SIZE][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][SIZE=2] { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] member1; } }[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] Member2 { [/SIZE][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][SIZE=2] { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] member2; } }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Program[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Main([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2][] args)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af] B[/COLOR][/SIZE][SIZE=2] bInst = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]B[/COLOR][/SIZE][SIZE=2]();[/SIZE]
[SIZE=2][COLOR=#0000ff] foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]PropertyInfo[/COLOR][/SIZE][SIZE=2] propInfo [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] bInst.GetType().GetProperties([/SIZE][SIZE=2][COLOR=#2b91af]BindingFlags[/COLOR][/SIZE][SIZE=2].Public |[/SIZE]
[SIZE=2][COLOR=#2b91af]BindingFlags[/COLOR][/SIZE][SIZE=2].Instance))[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (propInfo.PropertyType == [/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2]) ||[/SIZE]
[SIZE=2] propInfo.PropertyType.IsSubclassOf([/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2])))[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#2b91af] MethodInfo[/COLOR][/SIZE][SIZE=2] method = propInfo.PropertyType.GetMethod([/SIZE][SIZE=2][COLOR=#a31515]"Method"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Type[/COLOR][/SIZE][SIZE=2][] { [/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2]) });[/SIZE]
[SIZE=2] method.Invoke(propInfo.GetValue(bInst, [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]), [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2][] { [/SIZE][SIZE=2][COLOR=#a31515]"Optional Message to A.Method"[/COLOR][/SIZE][SIZE=2] });[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Method([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2] Message)[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#2b91af] Console[/COLOR][/SIZE][SIZE=2].Out.WriteLine([/SIZE][SIZE=2][COLOR=#a31515]"Call to A.Method(\"{0}\")"[/COLOR][/SIZE][SIZE=2], Message ?? [/SIZE][SIZE=2][COLOR=#a31515]"<N/A>"[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]B[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff] private [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2] member1 = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] [SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE]();[/SIZE]
[SIZE=2][COLOR=#0000ff] private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] member2 = 0;[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2] Member1 { [/SIZE][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][SIZE=2] { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] member1; } }[/SIZE]
[SIZE=2][COLOR=#0000ff] public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] Member2 { [/SIZE][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][SIZE=2] { [/SIZE][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][SIZE=2] member2; } }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Program[/COLOR][/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Main([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2][] args)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#2b91af] B[/COLOR][/SIZE][SIZE=2] bInst = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]B[/COLOR][/SIZE][SIZE=2]();[/SIZE]
[SIZE=2][COLOR=#0000ff] foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]PropertyInfo[/COLOR][/SIZE][SIZE=2] propInfo [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] bInst.GetType().GetProperties([/SIZE][SIZE=2][COLOR=#2b91af]BindingFlags[/COLOR][/SIZE][SIZE=2].Public |[/SIZE]
[SIZE=2][COLOR=#2b91af]BindingFlags[/COLOR][/SIZE][SIZE=2].Instance))[/SIZE]
[SIZE=2][COLOR=#0000ff] if[/COLOR][/SIZE][SIZE=2] (propInfo.PropertyType == [/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2]) ||[/SIZE]
[SIZE=2] propInfo.PropertyType.IsSubclassOf([/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#2b91af]A[/COLOR][/SIZE][SIZE=2])))[/SIZE]
[SIZE=2] {[/SIZE]
[SIZE=2][COLOR=#2b91af] MethodInfo[/COLOR][/SIZE][SIZE=2] method = propInfo.PropertyType.GetMethod([/SIZE][SIZE=2][COLOR=#a31515]"Method"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#2b91af]Type[/COLOR][/SIZE][SIZE=2][] { [/SIZE][SIZE=2][COLOR=#0000ff]typeof[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2]) });[/SIZE]
[SIZE=2] method.Invoke(propInfo.GetValue(bInst, [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]), [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2][] { [/SIZE][SIZE=2][COLOR=#a31515]"Optional Message to A.Method"[/COLOR][/SIZE][SIZE=2] });[/SIZE]
[SIZE=2] }[/SIZE]
[SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE]
Большое всем спасибо !