473,498 Members | 703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inherit attribute with interface ?

Hi everybody,

I have write customize attribute TestAttribute

[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class TestAttribute : Attribute
{
private string name;
public TestAttribute(string name)
{
this.name = name;
}
}

[TestAttribute("Test test")]
public interface TestInterface
{
void function();
}

public class TestClass : TestInterface
{
public void function()...
}

But why TestClass does not inherit TestAttribute from TestInterface ???

Type t = typeof(DerivedClass);

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute));

testAttr alway null. Can anybody help me ?
Thanks.
--
Vi
Nov 15 '05 #1
3 2198
Did you try

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute), true);

[Note the "true" at the end of the call]

The true tells the method to look for this attribute in the full inheritance
chain.

-vJ

"TruongLapVi" <tr*********@hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have write customize attribute TestAttribute

[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class TestAttribute : Attribute
{
private string name;
public TestAttribute(string name)
{
this.name = name;
}
}

[TestAttribute("Test test")]
public interface TestInterface
{
void function();
}

public class TestClass : TestInterface
{
public void function()...
}

But why TestClass does not inherit TestAttribute from TestInterface ???

Type t = typeof(DerivedClass);

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute));

testAttr alway null. Can anybody help me ?
Thanks.
--
Vi

Nov 15 '05 #2
Thanks you for answer,
I have try it, but the result is still "null" :(.

"Vijaye Raji" <no************@hotmail.com> wrote in message
news:eK**************@tk2msftngp13.phx.gbl...
Did you try

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute), true);

[Note the "true" at the end of the call]

The true tells the method to look for this attribute in the full inheritance chain.

-vJ

Nov 15 '05 #3
100
Hi TruongLap,
It seems like attributes cannot be inherited from interfaces. If you try and
make TestInterface class instead of interface you will find out that
everything works how it suppose to.

B\rgds
100

"TruongLapVi" <tr*********@hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
Hi everybody,

I have write customize attribute TestAttribute

[AttributeUsage(AttributeTargets.All, Inherited = true)]
public class TestAttribute : Attribute
{
private string name;
public TestAttribute(string name)
{
this.name = name;
}
}

[TestAttribute("Test test")]
public interface TestInterface
{
void function();
}

public class TestClass : TestInterface
{
public void function()...
}

But why TestClass does not inherit TestAttribute from TestInterface ???

Type t = typeof(DerivedClass);

TestAttribute testAttr = (TestAttribute)Attribute.GetCustomAttribute(t,
typeof(TestAttribute));

testAttr alway null. Can anybody help me ?
Thanks.
--
Vi

Nov 15 '05 #4

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

Similar topics

2
9230
by: Microsoft | last post by:
I have Interface defined with custom attribute. My class implements the same interface , but I'm not able to see any attributes derived from interface.( I did set the inherited to true) .
5
1741
by: john conwell | last post by:
I'm trying to make a class that already inherits from a base class, also inherit from an interface in managed C++. so my interface looks like so: __gc interface ITask { __property String*...
2
2663
by: Christoph Heindl | last post by:
Hi, I'm currently struggling with a design decision. One of my classes (named Attributable) has a protected member of type std::list<StrongReference<Attribute> > Basically the list has...
19
2408
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
0
7124
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
7163
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
7200
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...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.