|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
PropertyInfo.GetValues(...) with Generics / ReflectionA sample property is as follows: /// <summary> /// Gets the collection of links rendered at the bottom of the header. /// </summary> /// <value>The links collection.</value> [DefaultValue((string)null)] [PersistenceMode(PersistenceMode.InnerProperty)] [Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [Category("Appearance")] [Bindable(true)] [ContainsLocalisationKeys(true)] public List<CompanyNameHyperLink> Links { get { return m_links; } } I am reflecting on this property to do some automatic localisation, using a few attributes, etc. I don't seem to be able to get .GetValue working with properties that use an index (Collections, etc.) or Generics, such as the property above. Can someone give me a line of code to get, for example List<CompanyNameHyperLink> from the propertyInfo.GetValue() method? For example (though this won't work): PropertyInfo propertyInfo = myReflectedProperty; // Properly defined elsewhere List<CompanyNameHyperLink> = (List<CompanyNameHyperLink>)propertyInfo.GetValue(this, null); I know that the propertyInfo is a List<> and I know it's attributes (CompanyNameHyperLink) ... it's just the last little piece of pulling the property values out of the PropertyInfo object. Any help would be appreciated, since I've spent hours on this to no avail, Christophe >I know that the propertyInfo is a List<> and I know it's attributes So what happens? Does it return null, throw and exception or something>(CompanyNameHyperLink) ... it's just the last little piece of pulling the >property values out of the PropertyInfo object. else? Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. I always get the error 'Object does not match target type'
Show quote "Mattias Sjögren" wrote: > >I know that the propertyInfo is a List<> and I know it's attributes > >(CompanyNameHyperLink) ... it's just the last little piece of pulling the > >property values out of the PropertyInfo object. > > So what happens? Does it return null, throw and exception or something > else? > > > Mattias > > -- > Mattias Sjögren [C# MVP] mattias @ mvps.org > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com > Please reply only to the newsgroup. > I just created a simple test project but I didn't see the problem:
class LinkControl { private List<CompanyNameHyperLink> m_links; public LinkControl() { m_links = new List<CompanyNameHyperLink>(); CompanyNameHyperLink l = new CompanyNameHyperLink(); m_links.Add(l); } [DefaultValue((string)null)] [PersistenceMode(PersistenceMode.InnerProperty)] [Editor("System.Web.UI.Design.WebControls.ListItemsCollectionEditor,System.D esign, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] [Category("Appearance")] [Bindable(true)] public List<CompanyNameHyperLink> Links { get { return m_links; } } } ...... LinkControl lc = new LinkControl(); PropertyInfo p = (typeof(LinkControl)).GetProperty("Links"); List<CompanyNameHyperLink> ll=(List<CompanyNameHyperLink>)p.GetValue(lc, null); Regards, Luke Zhang Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== (This posting is provided "AS IS", with no warranties, and confers no rights.) I can access the property in the way you demonstrate in code ... what I need
is to get the value of the property (return the collection) using the ..GetValues method of the PropertyInfo class (in System.Reflection). I can't figure out how to get the values for a Generic collection ... this works fine for normal properties, though. What I do is cycle through all properties in a class, looking for properties that implement a certain custom Attribute (ContainsLocalisationKeys). If I find this attribute, I try to get the values of this generic collection and localise them. Thus the need to retrieve the collection from the PropertyInfo .GetValues method. Christophe. Luke:
I see my problem ... I was passing the wrong object in the GetValue parameters (passing the page object not the specific control). Sorry for wasting your time on a mistake that was my own fault. Christophe I am glad to hear that the problem is resolved. :)
Regards Luke Zhang Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== (This posting is provided "AS IS", with no warranties, and confers no rights.) |
|||||||||||||||||||||||