0

I am using C# to create a view model that I later serialize into Json for use with KnockoutJs.

Now I need to add information on a property level if a certain user has access to view and/or edit the property.

Since Javascript has little to no reflection possibilities I want to do this with C# before serializing to Json.

My view model could look like this:

class ViewModel {
    public string Name { get; set; }        
}

I want to use a User Access Service that would take the user, which has some Roles, and the view model and go through the Roles and apply the access rights. Something like this:

class AccessService {
    public void Apply(IUser user, ViewModel viewModel) {
        if(user.Roles.Contains(Roles.Admin)) {
            viewModel.AllowRead(vm => vm.Name);
        }
    }
}

The viewModel.AllowRead(..) method would be an extension method that would take an object (or maybe an interface or type if necessary) and this is where the magic would happen.

I would like the result of this operation to be that the viewModel would get a new property with the name CanRead which in turn would have a boolean property with the name Name.

The resulting viewModel would look like this:

class ViewModel {
    public string Name { get; set; }
    public object CanRead { // Could non anonymous as well.
        return new {
            Name = true;
        };
    }
}

Can this be done with dynamics or do I have to use Reflection.Emit? I'm not asking for "show me the codez". I just want to know if my idea is mental or if it's possible.

Any ideas?

2
  • Is this ViewModel used in WPF? Commented Feb 13, 2012 at 16:46
  • No, it is used with KnockoutJs which is a MVVM thing for Javascript, hence the JSON. Commented Feb 13, 2012 at 16:48

1 Answer 1

1

I think it would be possible and you could use the Lambdanator to help achieve this.

Example usage:

Lambda.Reflect<SomeClass>(x => x.AMethod());    
Lambda.Reflect<SomeClass>(x => x.AProperty);    
Lambda.Reflect(() => local);
Sign up to request clarification or add additional context in comments.

3 Comments

While this would help me getting existing properties from a type based on a Lambda expression it doesn't help me in the Reflection.Emit, dynamic or whatever thing that would be needed to reach my goal. Still interesting though. Thanks!
Yes it's just the first step on the walk. I think what you're asking for is possible with Reflection & dynamics but I haven't tried.
I've solved it now. It's beautiful! I used a dynamic ExpandoObject on a ViewModelBase class that is exposed as a property. Then I stole some Expression magic from Lambdanator, basically just the Expression<Func<T, object>>, and created an extension method that adds a property to the dynamic object using ((IDictionary<String, Object>)theDynamicProperty)[memberAccess.Member.Name] = true;. It works really well! Thanks for the hint with the Lambdanator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.