This question is similar to this one: Restrict method access to a specific class in C++
However, my question is for C#.
To reiterate: I have two classes, say Foo
and Bar
which are tightly coupled. Foo
exposes a method that I only ever want Bar
to have access to.
I kinda have an idea of what I want but no idea how to actually do it, or know if a simpler way exists. It would involve using some kind of attribute that relies on using (new StackFrame(1)).GetMethod().DeclaringType
to deny access. Usage would look something like this:
public class Foo {
[RestrictedUsage(Allow=typeof(Bar))]
public int SomeMethod()
{
// do something
}
}
There are probably relevant details I'm missing for the question (I'm new here) - I'll be sure to update it as I go.
UPDATE:
I should mention that Foo
wraps an existing UIElement
who's behavior I am trying to modify. Since the class is sealed, it's difficult to modify it normally. Bar
is the class that transforms the values into the correct values. Bar
is non-UI and is used other places in my application. Foo
doesn't know how to transform the value (it's complex - thats what Bar
is for) and that's why I can't just wrap the base method.
Jon Skeet gave the answer I was expecting - change the layout of the classes (all thanks be to him so far) but I don't think either answer is applicable (good answers just not relevant).
I've already made mistakes in the code, referencing the wrong property...