Creating restricted Methods/Fields in C#
<p>This question is similar to this one:
<a href="http://stackoverflow.com/questions/1843221/restrict-method-access-to-a-specific-class-in-c">http://stackoverflow.com/questions/1843221/restrict-method-access-to-a-specific-class-in-c</a></p>

<p>However, my question is for C#.</p>

<p>To reiterate: I have two classes, say <code>Foo</code> and <code>Bar</code> which are tightly coupled. <code>Foo</code> exposes a method that I only ever want <code>Bar</code> to have access to.</p>

<p>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 <code>(new StackFrame(1)).GetMethod().DeclaringType</code> to deny access. Usage would look something like this:</p>

<pre><code>public class Foo {

    [RestrictedUsage(Allow=typeof(Bar))]
    public int SomeMethod() 
    {
        // do something
    }

}
</code></pre>

<p>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.</p>

<p><strong>UPDATE:</strong>
I should mention that <code>Foo</code> wraps an existing <code>UIElement</code> who's behavior I am trying to modify. Since the class is sealed, it's difficult to modify it normally. <code>Bar</code> is the class that transforms the values into the correct values. <code>Bar</code> is non-UI and is used other places in my application. <code>Foo</code> doesn't know how to transform the value (it's complex - thats what <code>Bar</code> is for) and that's why I can't just wrap the base method.</p>

<p>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).</p>

<p>I've already made mistakes in the code, referencing the wrong property...</p>
