473,386 Members | 1,841 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,386 software developers and data experts.

Why can't you create a Generic type that inherits from Attribute?

Wes
Why can't you create a Generic type that inherits from Attribute? Of course I'm talking about in C# 2.0 and .Net Framework 2.0.

I posted this question on my weblog (http://weblogs.asp.net/whaggard/arch...12/241476.aspx) last week but I no one gave an answer. So I figured I would post it to the C# newsgroup to see if anyone can shine some light on this question for me.

Thanks
Wes Haggard
http://weblogs.asp.net/whaggard/

Nov 16 '05 #1
3 1316
Wes,

Consider the following:

public class MyAttribute<T> : Attribute
{
}

[MyAttribute<MyClass>]
public MyClass
{
}

The compiler wouldn't know what to do, as it would have to have a type
definition for MyClass to create the metadata for MyClass, but the MyClass
type definition isn't complete without MyAttribute<T> being resolved.

You could use a compiler check to make sure that this case wasn't
applied, but I think it is a good idea that they didn't allow this for now.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Wes" <ne********@puzzleware.net> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Why can't you create a Generic type that inherits from Attribute? Of
course I'm talking about in C# 2.0 and .Net Framework 2.0.

I posted this question on my weblog
(http://weblogs.asp.net/whaggard/arch...12/241476.aspx) last week
but I no one gave an answer. So I figured I would post it to the C#
newsgroup to see if anyone can shine some light on this question for me.

Thanks
Wes Haggard
http://weblogs.asp.net/whaggard/

Nov 16 '05 #2
Wes
Hello Nicholas Paldino [.NET/C# MVP],

Thanks for the response.

How is your example different from this (which is allowed):

public class MyClass
{
public List<MyClass> Get() { };
}

Thanks,
Wes Haggard
http://weblogs.asp.net/whaggard/

Wes,

Consider the following:

public class MyAttribute<T> : Attribute
{
}
[MyAttribute<MyClass>]
public MyClass
{
}
The compiler wouldn't know what to do, as it would have to have a
type definition for MyClass to create the metadata for MyClass, but
the MyClass type definition isn't complete without MyAttribute<T>
being resolved.

You could use a compiler check to make sure that this case wasn't
applied, but I think it is a good idea that they didn't allow this for
now.

Hope this helps.

"Wes" <ne********@puzzleware.net> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Why can't you create a Generic type that inherits from Attribute? Of
course I'm talking about in C# 2.0 and .Net Framework 2.0.

I posted this question on my weblog
(http://weblogs.asp.net/whaggard/arch...12/241476.aspx) last
week but I no one gave an answer. So I figured I would post it to
the C# newsgroup to see if anyone can shine some light on this
question for me.

Thanks
Wes Haggard
http://weblogs.asp.net/whaggard/


Nov 16 '05 #3

"Wes" <ne********@puzzleware.net> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
Why can't you create a Generic type that inherits from Attribute? Of
course I'm talking about in C# 2.0 and .Net Framework 2.0.

I posted this question on my weblog
(http://weblogs.asp.net/whaggard/arch...12/241476.aspx) last week
but I no one gave an answer. So I figured I would post it to the C#
newsgroup to see if anyone can shine some light on this question for me.

My hypothesis would be because Attributes are serialized into metadata that
must be serializable without initalizing any data(parameters are serialized
into the containing type), however that is certainly not much of an answer.

I did a little research but I can't find any documents that may help. I
don't think you are going to find a terribly concrete answer for a while
yet.

I can say that ILASM doesn't appear to allow you to create generic
attributes in IL either, but then I just may be screwing up the IL. Thanks
Wes Haggard
http://weblogs.asp.net/whaggard/

Nov 16 '05 #4

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

Similar topics

5
by: PJ | last post by:
I have a class defined as so: public class Pager : Control { private PagingList<T> itemList; .... I'm getting an error message that says, "The type or namespace name 'T' could not be found....
0
by: CrispinH | last post by:
Hi I've just upgraded some Type Editors from 1.1 to 2.0 and whilst they compile OK, I'm not getting any results in the test harness PropertyGrid. Usually when you select a particular property,...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
2
by: Andrew Backer | last post by:
I hope someone out there can help me with this, because I am stuck. My problem is that I have an instance of a generic class, but I don't know ahead of time what the type parameter used was. In...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.