PC Review


Reply
Thread Tools Rate Thread

custom attributes in .Net 2.0

 
 
=?Utf-8?B?d2luZHNpbQ==?=
Guest
Posts: n/a
 
      24th May 2007
Hi,
I have a custom attribute targing Property:

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}

and all the properties marked with [StatefulPropertyAttribute] will be
handled by

public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
return this.GetProperties(new Attribute[]{new
DataObject.StatefulPropertyAttribute()});
}

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);//bind object

globalizedProps = new PropertyDescriptorCollection(null);

// For each property use a property descriptor of our own that is able to be
globalized
foreach (PropertyDescriptor oProp in baseProps ) //show those
properties
{
if (!this.IsPropertyHidden(oProp.Name))
globalizedProps.Add(new GlobalizedPropertyDescriptor(oProp));
}
return globalizedProps;
}

They will be put into PropertyDescriptorCollection.This works fine under
..Net 1.1. But under .Net 2.0 it somehow does not work as the same way as
..net 1.1, that means those properties not marked with
[StatefulPropertyAttribute] are also put into the
PropertyDescriptorCollection.

So is there any changes ABOUT TypeDescriptor.GetProperties(this, attributes,
true)in .NET 2.0? How to solve this problem?

Thanks.
--
windsim
 
Reply With Quote
 
 
 
 
Marc Gravell
Guest
Posts: n/a
 
      24th May 2007
My test code (below) shows only a single property; therefore I'm
guessing there is something else in your code that you aren't showing
that is causing the problem; can you reproduce with postable code?
Note that the default "Match" / "Equals" implementation for Attribute
tests the fields (instance, public + private) for equality; have you
any fields that might be set? If so you may need to override Equals,
or do somthing different.

Also - in 2.0 this would suit a TypeDescriptionProvider implementation
very nicely; for a related example (ignore the Reflection.Emit stuff)
see http://www.codeproject.com/csharp/Hy...Descriptor.asp
This essentially allows you to intercept TypeDescriptor at a much
earlier point, allowing you to substitute / filter / append
roperties - in your case substitute would be quite nice.

using System;
using System.ComponentModel;
using System.Diagnostics;

static class Program {
static void Main() {
new TestClass().RunTest();
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false,
Inherited = true)]
public class TestAttribute : Attribute { }

public class TestClass {
public void RunTest() {
Attribute[] attribs = {new TestAttribute()};
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(this, attribs, true)) {
Trace.WriteLine(prop.Name);
}
}
public string Test1 {
get { return "abc"; }
}
[Test]
public string Test2 {
get { return "abc"; }
}
}
}


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom attributes Random Microsoft Dot NET Framework 5 15th Feb 2007 08:30 AM
WebControl.Attributes.Add and custom attributes P4trykx Microsoft ASP .NET 2 31st Jan 2007 04:33 PM
Help with custom attributes =?Utf-8?B?V2lsbCBXYWdnb25lcg==?= Microsoft ASP .NET 0 3rd Mar 2005 05:29 PM
about Custom Attributes! xiaorun huang Microsoft Dot NET Framework 1 25th Feb 2004 02:52 AM
Re: Add custom attributes to AD Russell greenwood Microsoft Windows 2000 Active Directory 1 25th Jul 2003 05:57 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:48 AM.