Similar Threads:
1.Using reflection on collections w/in objects
Hello,
I have a generic subroutine that I pass an object and
fieldname as arguments. The subroutine then uses
reflection to search for the value of the fieldname.
For example:
'Calling the Sub
GetObjectValue(objMyCustomer, "Address.Zip")
'The definition of the Sub
Public Shared GetObjectValue(MyObj as Object, FieldName As
String)
Dim Field As FieldInfo
Dim colObject As Object
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
Field = colObject.GetType.GetField(Left(strTemp, _
strTemp.IndexOf(".")))
If Field Is Nothing Then Return Nothing
strTemp = Mid(strTemp, strTemp.IndexOf(".") + 2, _
Len(strTemp) - strTemp.IndexOf("."))
colObject = Field.GetValue(colObject)
Loop
'A bunch of other junk goes here
End Sub
The logic works, there's a problem when what you're trying
to look for is in a collection. For instance, if I have
collection of Addresses, like home address, mailing
address, work address, etc.
I want to get the Zip code of, say, the work address.
When I call it, GetType.GetField successfully assigns
Field as a {System.Reflection.RuntimeFieldInfo}. I don't
see a way to test for Field (actually Address) being a
collection. Try to do a Ctype and then test for not
returning that particular error code?
I'd then like loop through my collection and return the
zip code for a particular element, like Address(1).Zip.
The reason I'm going to all this trouble is that I want
the piece that calls GetObjectValue not to have to know
what the type of object that it's working with. I just
give him an object and he parses through it looking for
fields in an XML document that is passed to him as well.
-Eric
2.How can i get the Property value using Reflection (Return type: Collection of controls)
i want to get the property value using reflection.
Scenerio:
i have a status bar on MDI form. it has property named "Panels" and i want
to get a specific panel from that
panels collection using reflection.
Please let me know.
Thanks
C# Developer
Mudassar
3.Datagrid columns using AutoGenerate via the autoGenColumnsArray private member array collection (reflection)
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>
4.Confused about using Reflection to examine a collection
5.Using Reflection with a collection class - syntax question
Hi,
I hope this is the right forum for this question.
I am trying to use reflection for several custom collection classes. For
example, what I want to do is take a line of code like this:
myCollection.Add(mySecondCollection.Item(i))
and rewrite it using reflection.
So far, I have something like
object myObject = new object();
object mySecondObject = new object();
myObject = myCollection;
mySecondObject = mySecondCollection;
myCollection.GetType().GetMethod("Add").Invoke(....
After the invoke is where I am confused about the syntax to use to represent
the second collection, and to represent the .Item syntax (.Item is a custom
method that returns an object in the collection and takes the collection
index as an argument).
I have read the documentation for .GetMethod, .Invoke, etc. and don't
understand what I'm supposed to do in this case.
Any help would be greatly appreciated.
Thanks!
--
E. Connolly
6. Using reflection on collections w/in objects
7. Inheriting VB6 dll classes/collections into dotnet classes/collections
8. Setting a VB6 Collection to a .NET Collection