1

enter image description here

hi,

I need to get the ColumnsNames in 'ResultColumns' as shown in the image of the quickwatch above.I am not able to get anything inside the 'query' bt can access it through the quickwatch as shown.How can i access it.Please Help.

Regards, Jaison

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Jaison John
  • 65
  • 1
  • 7
  • 1
    Sorry what?? I don't understand your question – RononDex Jan 20 '14 at 08:01
  • tried to access builder.query as shown above but its showing the corresponding error: 'Korzh.EasyQuery.Db.SqlQueryBuilder' does not contain a definition for 'query' nd no extension method 'query' accepting a first argument of type 'Korzh.EasyQuery.Db.SqlQueryBuilder' could be found (are you missing a using directive or an assembly reference?) but in quickwatch no issues? – Jaison John Jan 20 '14 at 08:09
  • As you see in your quick watch utility the field "query" is private (you can see that because of the small lock below the blue cube). This means you can't access it using normal accessors. You would have to use Reflection to access private fields – RononDex Jan 20 '14 at 08:26
  • thanku verymuch....can u help me doing this... – Jaison John Jan 20 '14 at 08:44

1 Answers1

3

Your field query is private. That is why you can't access it in a "normal" way.

You can still access it through Reflection tough. You can use the following code to access a private field:

/// <summary>
/// Uses reflection to get the field value from an object.
/// </summary>    ///
/// <param name="type">The instance type.</param>
/// <param name="instance">The instance object.</param>
/// <param name="fieldName">The field's name which is to be fetched.</param>
/// <returns>The field value from the object.</returns>
internal static object GetInstanceField(Type type, object instance, string fieldName)
{
    BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
        | BindingFlags.Static;
    FieldInfo field = type.GetField(fieldName, bindFlags);
    return field.GetValue(instance);
}

In your case you would then call it like:

Korzh.EasyQuery.Db.DbQuery query = GetInstanceField(typeof(Korzh.EasyQuery.Db.SqlQueryBuilder), builder, "query") as Korzh.EasyQuery.Db.DbQuery;

However you should do this only when absolutely necessary. Fields are normally private for a reason. And bypassing this my cause some unexpected behaviour.

The code for the function was taken from the following answer: How to get the value of private field in C#?

Community
  • 1
  • 1
RononDex
  • 4,143
  • 22
  • 39