abt System.Reflection

  • Thread starter Thread starter rajamanickam
  • Start date Start date
R

rajamanickam

hi,
I am rajamanickam.. working as a software developer....

i tried to create a general class in vb.net to access an
incoming parameter object's methods and properties.. by
Object.GetType.InvokeMethod in System.Reflection.It is
working and calling methods of the parameter object.But i
couldnot get values from properties of the incoming
parameter object.......

for an example.. -----------------------

public class gen_class

public function call_Method_fromChild(byref incomingObj as
object,byval methodName as string,byval arguments as
string)
incomingObj.getType.InvokeMethods(methodname.......)
end function

public function getValueFromChild(byref incomingObj as
object,byval propertyName as string)
dim propertyObj as system.reflection.PropertyInfo
propertyobj=incomingobj.getType.getProperty
(propertyname,Reflection.BindingFlags.Default)
propertyobj.getValue()
.....
...
end function
end gen_class

-------------- but the above.. coding is not working..and
not getting value from property... but method calling is
working..

if u know plz.. send me the points.. what mistake i
committed.. to use this great option.....
bye
Rajamanickam
 
dim propertyObj as system.reflection.PropertyInfo
propertyobj=incomingobj.getType.getProperty
(propertyname,Reflection.BindingFlags.Default)

If your property is public/private and static/instance [which it will be]

// If it is Public Instance property
propertyobj=incomingobj.getType.getProperty
(propertyname,Reflection.BindingFlags.Public | BindingFlags.Instance)

// If it is Public Static property
propertyobj=incomingobj.getType.getProperty
(propertyname,Reflection.BindingFlags.Public | BindingFlags.Static)

If it is private property, in the above statement, change BindingFlags.Public to BindingFlags.Private

This is what the doc says
Note You must specify Instance or Static along with Public or NonPublic or no members will be returned.


eg

class person
{
string name;

public person(string name)
{
this.name = name;
}

public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}


private void button4_Click(object sender, System.EventArgs e)
{
person p = new person("kalpesh");
PropertyInfo pi = p.GetType().GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);

string s = (string)pi.GetValue(p,null);
MessageBox.Show(s);
return;
}


HTH
Kalpesh
 
Back
Top