0

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.

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

will i be able to find the value of the property of the parent class. Because Command is the property of the base class CDatabase.
Yes, of course. If you do not specify BindingFlags.DeclaredOnly it will give you all properties of all classes up the hierarchy. Just lookup these methods on MSDN.
I am not able to solve my problem. In my class I have different methods. I want to know the value of the property Command.CommandText which is inside a particular method and command is declared in my parent class. Hope I am able to tell you my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.