简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM