How can i get the Property value using Reflection (Return type: Collection of controls)
by Mudassar » Sat, 09 Apr 2005 02:03:34 GMT
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
Re: How can i get the Property value using Reflection (Return type: Collection of controls)
by Pete Davis » Sat, 09 Apr 2005 03:07:48 GMT
Funny you should ask today. There's been a lot of reflection going on in our
office today. Here's a sample of getting a field:
string dataField;
FieldInfo fi = currentManager.GetType().GetField("dataField",
BindingFlags.NonPublic | BindingFlags.Instance);
dataField = (string) fi.GetValue(currentManager);
Basically, you'll want to get ProeprtyInfo instead of a FieldInfo (using
GetProperty()), but otherwise it's more or less the same.
In this case, currentManager is a "CurrencyManager" object and "dataField"
is a private field in the CurrencyManager.
The key is to make sure your BindingFlags values are correct. These should
work for non-public instance fields. If it's static or public (I doubt it's
public, otherwise why use reflection, right?), make the appropriate changes.
Hope that helps.
Pete
want
Re: How can i get the Property value using Reflection (Return type: Collection of controls)
by Mudassar Khan » Wed, 13 Apr 2005 00:35:45 GMT
Pete Davis,
Thanks for the reply and sample code.
i solved my requirement in the following way
object MainForm = this.GetMainForm();
object statusBar =
MainForm.GetType().InvokeMember("StatusBar",BindingFlags.GetProperty,nul
l,MainForm,null);
IList panels = (IList)statusBar.GetType().GetProperty
("Panels").GetGetMethod().Invoke(statusBar,null);
object progressPanel = panels[1];
progressPanel.GetType().GetProperty("Visible").SetValue(progressPanel,tr
ue,null);
this is how i get the List and get a specific object from list and set
its property.
Regards
Mudassar
*** Sent via Developersdex http://www.**--****.com/ ***
Similar Threads:
1.Getting list of Properties using Reflection
Hi,
I'm trying to get a list of properties from a hierarchy of classes,
using reflection.
Using Type.GetProperties(), I get the list back in "reverse hierarchy
order" (derived class properties before parent class properties)
Using TypeDescriptor.GetProperties(), the order of the returned
properties is also wrong.
I used the XmlSerializer to test my classes, and the properties was
written out correctly (Parent class before derived class).
What can I do to get a list of properties in the correct order (Parent
class before inherited class)?
Thanks for the help,
Dawid Mostert
XXXX@XXXXX.COM
2.Assign Property value using Reflection
assuming that I have a class
public class MyClass {
public string FirstName {
get {...}
set {...}
}
}
how can I assign the FirstName property using reflection on a generic class
of type MyClass?
string propertyName = "FirstName";
C is a gerneric type of type "MyClass"
so what I want to do is:
C.propertyName = "Andrew"; // <-pseudo code...
For the simplicity, lets assume that the property is always of type string
but it could very (an I will know the type ahead of time.)
Thanks,
--
Andrew Robinson
3.Using Enumerated Values to Access Property Collection
Hi,
I have an enum type as defined below. The enum constants are some of
the properties for a windows form control. I want to store the
settings for that control into a file. In order to access the value of
the properties (for example toolbar.Size or Toolbar.Name) I used a for
loop. In my for loop, I want to do something like
toolbar.[Enum](Prop.GetName)(which is equal to toolbar.Size and
returns the value into the string)
Public Enum ToolBarSettings
Name
Size
Location
End Enum
Dim Properties As Type = GetType(ToolBarSettings)
For Each Prop In [Enum].GetNames(Properties)
Dim s as string = toolbar.[Enum](Prop.GetName) ' I want
this to return toolbar.size into the string)
Next
Is there a way that I can do this?
Thank you
Gos
4.server control collection with several types of properties
Is it possible to implement a server control that will look like this:
<just:control>
<columns>
<columnTypeA id=1></columnTypeA>
<columnTypeA id=2></columnTypeA>
<columnTypeB id=3></columnTypeB>
<columnTypeB id=4></columnTypeB>
</columns>
</just:control>
While columnTypeA is a collection and columnTypeB is a different collection?
Another option is that both tags will be in the same collection (if
possible..)
Thanks.
5.Setting the value of a Master page's control's property using a property of the Master page
6. Error getting Identity Value using Strongly Typed dataset
7. Using Reflection to get a Field Value from Control
8. Getting ID, calling url, search for value, return value