Click to See Complete Forum and Search --> : How to create a class which can control it's inherited attribute?


xred
May 5th, 2006, 08:50 PM
e.g.:

class Test
{
[Attribute_Test(true)]
public int function1()
{
....;
}
}

class Test2 : Test
{
}


Now, I want to control Test2's Attribute by some property, for example:


class Test2 : Test
{
private bool m_attribute_test;
public bool Attribute_Test
{
get {}
set {}
}
}


How to do that? I'm not good at English, could you understand what I said?

jhammer
May 6th, 2006, 05:55 AM
you should use reflection. The method you are looking for is GetCustomAttributes.

In order to set the attribute at run-time, I don't know of a way to do it, and I don't think it is possible.

xred
May 6th, 2006, 07:04 AM
Is it impossible?
......

savagerx
May 6th, 2006, 07:22 AM
you should use reflection. The method you are looking for is GetCustomAttributes.

In order to set the attribute at run-time, I don't know of a way to do it, and I don't think it is possible.

How about this one?

Attribute.GetCustomAttributes (Assembly, Type, Boolean)

Retrieves an array of the custom attributes applied to an assembly. Parameters specify the assembly, the type of the custom attribute to search for, and an ignored search option.
Supported by the .NET Compact Framework.

http://msdn2.microsoft.com/en-us/library/system.attribute.getcustomattributes.aspx

xred
May 8th, 2006, 12:59 AM
Hi, savagerx, thanks very much!
Yes, now I can get the custom attributes, but if I want to modify it at the run time, how to do it?
Is it possible?
Thanks again!

jhammer
May 8th, 2006, 02:26 AM
Why do you need to change the attributes at run-time. It doesn't make sense. Are properties not enough?

xred
May 9th, 2006, 10:21 PM
Ah, just study!

jhammer, thanks!