Altering lazy-loaded object's private variables

I'm running into an issue with private setters when using NHibernate and lazy-loading. Let's say I have a class that looks like this:

public class User
    public virtual int Foo {get; private set;}
    public virtual IList<User> Friends {get; set;}

    public virtual void SetFirstFriendsFoo()
        // This line works in a unit test but does nothing during a live run with
        // a lazy-loaded Friends list
        Friends(0).Foo = 1; 

The SetFirstFriendsFoo call works perfectly inside a unit test (as it should since objects of the same type can access each others private properties).

However, when running live with a lazy-loaded Friends list, the SetFirstFriendsFoo call silently fails. I'm guessing the reason for this is because at run-time, the Users(0).Foo object is no longer of type User, but of a proxy class that inherits from User since the Friends list was lazy-loaded.

My question is this: shouldn't this generate a run-time exception? You get compile-time exceptions if you try to access another class's private properties, but when you run into a situation like this is looks like the app just ignores you and continues along its way.

Note: If I change the "private set" to a "protected set", everything works fine.


As Paco points out only virtual methods/properties can be proxied, and C# does not allow virtual private members.

You could possibly access it via reflection, but my first thought would be that you should try to avoid it. Nonetheless, if you want to go there you need something along these lines:

// edit: fixed to use GetType
Type type = Friend[ 0 ].GetType().BaseType;
type.GetProperty("Foo",BindingFlags.Instance|BindingFlags.NonPublic).SetValue( Friends[0], 1, null );

Alternatively, you can use Fasterflect and do the same like this:

Friends[ 0 ].SetPropertyValue( "Foo", 1 );

Need Your Help

VB.NET disable the keyboard and mouse

c# windows-7

I am looking to create a VB.NET application. when the application is opened it will disable the keyboard and mouse so if you hit any keys it does nothing and you can not move the mouse. (even if

Some html code dissapears when we use request in Python

python html python-2.7 web-crawler

I am crawling some data from a website. I need to recover some links from a list of products. First I identified one of the links with inspect element: