Getting class variable value using reflection
In my business logic I have created classes for database operations like insert, update etc. For this purpose I have created a class CDatabase which sets has some methods define in it like openconnection and closeconnection transation etc.
Now my logic class inherit that class
CAnswerLogic : CDatabase
{
OpenConnection();
BeginTrans();
Command.CommandText = "PKG_ANSWER.PROC_ADD_ANSWERS";
}
Can I get the value of Command.CommandText using reflection. Command is a property inside CDatabse class.
I have written a method to return all the method of a class
private IEnumerable<string> GetAllMethod(string pstrClassName)
{
const BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
var llistMethod = new List<string>();
var assembly = Assembly.LoadFile(Server.MapPath(@"bin/InfoDomeBLL.dll"));
try
{
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && type.Name == pstrClassName)
{
var method = type.GetMethods(flags);
foreach (var methodInfo in method)
{
llistMethod.Add(methodInfo.Name);
//var mb = methodInfo.GetMethodBody();
//foreach (LocalVariableInfo lvi in mb.LocalVariables)
//{
// Response.Write("Local variable: " + lvi);
//}
}
var basetype= type.BaseType;
}
}
}
catch (Exception)
{
}
return llistMethod;
}
In the web project i have added the reference of the bll project. Kindly help me out.
If you use type.GetProperties(flags);
instead of type.GetMethods(flags);
you will find the property you are looking for. Then, do propertyInfo.GetValue( Command, null );
to get the value.
上一篇: 请求签名与使用Python为Amazon AWS提供的签名不匹配
下一篇: 使用反射获取类变量值