ShantanuAggarwal

Hi,,

I have one following question that whether attributes can help me in resolving this problem:

I have one base class called Employee
and I have 3 classes which inherit it:
EmployeeA
EmployeeB
EmployeeC


Now, the base Employee class contain 3 methods:
void funct1() : needed for EmployeeA & EmployeeB
void funct2() : needed for EmployeeB & EmployeeC
void funct3() : needed for EmployeeC & EmployeeA


Since all the above three methods will be protected in base class, each of them can be called by any inherited class.

Now, I wanted to know, if I can use attributes here for these 3 methods, which will throw an error if the function is called from the wrong class, for e.g., if funct1() is called from EmployeeC it should give a compile time error.

Would really appreciate any help in this regards.

Warm regards,
Shantanu


Re: Visual C# General Can Attributes help in this situation ?

Matthew Watson

No, I don't think attributes will really help here.

However, in your base class' implementation of, for example, funct1(), you could put at the start:

Debug.Assert((this is EmployeeA) || (this is EmployeeB));





Re: Visual C# General Can Attributes help in this situation ?

ShantanuAggarwal

Hi ..

Thanks for your response..

Actually I think this will detect the condition at runtime... but the I am looking for a solution that can check this during compile time itself.. I am searching for possible solutions..but have not succeeded yet... anyways.. will appreciate any help on this.




Re: Visual C# General Can Attributes help in this situation ?

TilakGopi

Hi,

U can use member variable to determine which derived class is calling the base class's method.

Have a char variable(public),and in the child class it self(which is about to call the base class method)

set it to 'A' ,if derivedA calls the method,

set it to 'B',if derivedB calls the method,

set it to 'C',if derivedC calls the method,

so by checking this variable's value, either u can allow or disallow the execution of particular function.

Is this clear Hope this will work as u wanted.Feel free to getback to me any issues on this further.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General Can Attributes help in this situation ?

ShantanuAggarwal

Hi

Many Thanks for your response...

hmm.. your solution is fine... but again it will work only at run time...

I have to design something... in which this can be detected at compile time itself.. for e.g. using attributes if define a method as obsolete, its detected at compile time.. can I design an attribute to handle this situation also in the same manner

Thanks
Warm regards,
Shantanu




Re: Visual C# General Can Attributes help in this situation ?

Matthew Watson

As far as I am aware, there is nothing you can do with attributes that will help you.

However, it should be possible to do something with CodeAnalysis/FXCop.

You can create your own custom rules for those, but it's quite complicated and not something I can really explain here.

There's an FXCop forum here on these MSDN forums where people might be able to help you.