Interface Properties with Custom Attributes

  • Thread starter Thread starter Microsoft
  • Start date Start date
M

Microsoft

Hello there,

I have following situation , how can I solve this puzzle in
C#.

I have interface called IVendor with some custom attributes applied to it

interface IVendor
{
[LengthValidator("Vendor
Name",FieldLength.Vendor),RequiredValidator("Vendor Name")]
string Name
{ get;set}

[LengthValidator("Vendor
Number",FieldLength.Vendor),RequiredValidator("Vendor Number")]
string Number
{
get;set;
}

}


Q 1. I have clas called Vendor , Which implement IVendor interface

Vendor : IVendor
{

}

after I implement all the interface , when I try to get the custom
attributes defined in IVedndor interface thru reflection,
I don't see any of my attributes. How can I inherit my custom attribute from
Interface to the class which implements the interface(Eventhoug it's Meta
data) ?

Q 2 : I have another class callled Invoice Which also implements IVendor
Interface and contains Vendor as inner class,

public class Invoice : IVendor
{
private Vendor _vendor;

}

How can I forward all the properties defined in IVendor infterface to inner
Vendor class peoperties, without reimplementing those interface in Invoice
class?

Thanks
Baski
 
Sorry this is a bug which is fixed in the next version of CLR.
Basically we were unable to get CustomAttribute on PropertyInfo and
EvenInfo on inheritance hierarchy.

You can try to define your own wrapper of GetCustomAttribute on
property so that it will walk the inheritance hierarchy by using
reflection APIs.

Sorry about this.

Yiru
 
3 years on and there's still no solution to this. Lets assume that writing a custom GetCustomAttributes is not an option. What can I do now to force the derived class's property to inherit the attributes of the parent/interface property.
 
Back
Top