Similar Threads:
1.Answer/HowTo : Accessing columns collection via the private autoGenColumnsArray property in a DataGrid using AutoGenerate
I have seen several people looking for a way to access the Columns
collection when using the AutoGenerate = true option. Some
people have gotten so far as to find the private autoGenColumnsArray
that has the information, but we as developers have no way to access
this information.
I have come up with a solution for the problem, (as I am sure many
others have) using reflection. Here is some sample code that will
print out the auto generated column names from a datagrid.
As stated, this code uses reflection. Reflection is slow. It might not
also be allowed depending on your system's security settings. Also, if
MS changes the internal structure of the DataGrid, this might break.
(We are breaking encapsulation)
Provided, is a function that will generically allow you to get the
value of any private member of a class using reflection.
Sorry about the formatting, I dont know a good way to format code for
usenet. Visual Studio should clean up the code just find for you tho.
<code>
DataGrid dg = new DataGrid();
dg.DataSource =new DataTable();// (Run some query or whatnot here)
dg.DataBind
ArrayList AutoGeneratedColumns
= (ArrayList) GetPrivateField(dg,"autoGenColumnsArray") ;
if (AutoGeneratedColumns!= null)
foreach (DataGridColumn CurrentColumn in AutoGeneratedColumns)
{
Response.Write(CurrentColumn.HeaderText);
}
public static object GetPrivateField(object PassedObject, string
FieldName)
{
object Field=null;
if (PassedObject == null)
throw new ArgumentNullException("PassedObject"
,"PassedObject must be an instantiated object.");
if (FieldName == null || FieldName.Trim() == "")
throw new ArgumentOutOfRangeException("FieldName"
,"Fieldname must be a non empty string.");
Type ObjectType = PassedObject.GetType();
System.Reflection.FieldInfo PrivateField =
ObjectType.GetField(FieldName
,System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.IgnoreCase);
if (PrivateField == null)
throw new ArgumentOutOfRangeException("FieldName"
, ObjectType.FullName + " does not have a field : " + FieldName +
".");
Field = PrivateField.GetValue(PassedObject);
return Field;
}
</code>
2.Having problem getting private members using reflection.
I have an ASP.NET application that is calling a custom class that is trying
to parse all of the members of my Page object using Type.GetMembers(). The
problem that I am having is that private members are not returned. I did
some digging and the MSDN documentation states that the caller must have
ReflectionPermission in order to get the private members of a class. I am a
little unfamiliar with this stipulation. I have checked the docs on
ReflectionPermission, but the examples do not make much sense. Could
someone please clarify on what I need to do in order for my code to be able
to parse private members of my Page using reflection?
--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
EmailID = varnk
Domain = Diebold.com
-----------------------------------
3.How to get private class members using reflection
Hi,
I'm trying to get some private class members using reflection, but am having
trouble: Example
using System;
using System.Reflection;
public class Customer
{
private string _fname;
public string FirstName
{
get {return _fname;}
set {_fname = value;}
}
private void GetMyMembers ()
{
Type t = this.GetType();
MemberInfo[] mInfo = t.GetMembers();
}
foreach(MemberInfo m in mInfo)
{
members += m.Name + " ";
Console.WriteLine(m.Name);
}
}
my output shows
get_FirstName
set_FirstName
what I want is _fname.
Any ideas?
Thanks,
Opa
4.Adding Focus to new row to a DataGrid using Autogenerated Columns
5.How to use Reflection to access private members?
I have a Sky class with the private property Isblue
I am trying to access this through reflection (NOT through the Sky_Accessor
class vstudio can create):
var mySky=new Sky();
var skyType=typeof(mySky);
var skyIsBlueProp=skyType.GetProperty("IsBlue");
var skyIsBluePropGetter=skyIsBlueProp.GetGetMethod();
the last line fails because skyIsBlueProp is null.
Changing the IsBlue property to public solves the issue.
How can I access IsBlue even when it is private?
6. reposted: accessing private members through reflection in vstu
7. reposted: accessing private members through reflection in vstudio
8. Reflection, hiding private members?