简体   繁体   English

使用反射获取类变量值

[英]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. 在我的业务逻辑中,我为数据库操作(如插入,更新等)创建了类。为此,我创建了一个CDatabase类,该类的集合中定义了一些方法,如openconnection和closeconnection转换等。

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.CommandText的值吗? Command is a property inside CDatabse class. 命令是CDatabse类中的属性。

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. 在Web项目中,我添加了bll项目的参考。 Kindly help me out. 请帮我。

If you use type.GetProperties(flags); 如果使用type.GetProperties(flags); instead of type.GetMethods(flags); 而不是type.GetMethods(flags); you will find the property you are looking for. 您会找到想要的物业。 Then, do propertyInfo.GetValue( Command, null ); 然后,执行propertyInfo.GetValue( Command, null ); to get the value. 获得价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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