custom attributes in .Net 2.0

G

Guest

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.
 
M

Marc Gravell

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/HyperPropertyDescriptor.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"; }
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top