473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

custom attributes in .Net 2.0

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
May 24 '07 #1
1 1884
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"; }
}
}
}
May 24 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: ...
1
by: Bret Pehrson | last post by:
I've converted a non-trivial C++ library to managed, and get the following unhelpful linker error: Assignment.obj : error LNK2022: metadata operation failed (80131195) : Custom attributes are...
3
by: Edward Diener | last post by:
I understand the syntax of custom attributes, but I have no idea what they are supposed to do. Anyone care to give me a clue as to their functionality ?
1
by: Tamas Demjen | last post by:
I started to experiment with VC++ 2005 Beta1. So far everything went fine, and already have a working project, but soon I realized that the compiler was ancient (not supporting half of the C++/CLI...
3
by: The Developer | last post by:
Hi All, I have a web application where I am adding a custom attribute to my ASP.NET text box control and changing value of that attribute at client side using JavaScript. My problem is that...
0
by: Sanjay Pais | last post by:
This is an extension of an earlier post I have created a custom text box and compiled it and dropped it into my toolbox. However when I change the value of my custom property in design mode and...
9
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
1
camel
by: camel | last post by:
Apologies if foolish question, but I somewhat expected Custom Attributes (i.e. inheriting from System.Attribute) decorating class properties to come through via WSDL to the proxy the same as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.