2

I have code:

// entity
public class PermissInfo
{
    public int PermissValue { get; set; }
}

// used class
public class MenuPermiss
{
    public static readonly PermissInfo PermissView = new PermissInfo { PermissValue = 1 };
    public static readonly PermissInfo PermissEdit = new PermissInfo { PermissValue = 2 };
    public static readonly PermissInfo PermissDelete = new PermissInfo { PermissValue = 4 };
}

And implement code like:

// implement class: check permiss
public static class ImplementClass
{
    // used like: return CheckPermiss(MenuPermiss.PermissEdit);
    public static bool CheckPermiss(PermissInfo permiss)
    {
        // How to get "MenuPermiss" class info by "permiss" param

        return false;
    }
}

How can i get MenuPermiss CLASS by MenuPermiss.PermissEdit param?

minhhungit
  • 180
  • 4
  • 25
  • 2
    Please make your code compile first. A `bool` method cannot return a `string`, your `set;` is missing a semicolon, your method signature does not fit `DoSomething` and I'm fairly sure all the static stuff will cause issues as well. How will you pass a static class as a parameter to a method? It implies creating an instance. – Jeroen Vannevel Nov 19 '13 at 03:25
  • so you want to pass in a class/property as a string then retrive the class info based on that property and return a bool that is a string? – sa_ddam213 Nov 19 '13 at 03:27
  • 1
    http://stackoverflow.com/questions/10338018/how-to-get-a-property-value-using-reflection – Abin Nov 19 '13 at 03:27
  • Thanks for your comment. @sa_ddam213: I have edited, DoSomething method return false. We are only get MyClass info, no more :) – minhhungit Nov 19 '13 at 03:34
  • @Abin Mathew: Thanks, but I only wanna get class info instead of value of property – minhhungit Nov 19 '13 at 03:39
  • The problem is you are passing a string value into `DoSomething`, at the least you are going to have to pass in the PropertyInfo of MyProperty to get any information on the class it belongs to. – sa_ddam213 Nov 19 '13 at 03:42
  • @All: So so sorry for my code. I modified. Thanks for help :) – minhhungit Nov 19 '13 at 04:08

2 Answers2

1

It is not possible using this syntax:

ImplementClass.DoSomething(MyClass.MyProperty);

but possible with this one:

ImplementClass.DoSomething(() => MyClass.MyProperty);

Solution:

using System.Linq.Expressions;

public static class ImplementClass
{
    public static bool DoSomething<T>(Expression<Func<T>> propertyExpression)
    {
        var memberInfo = ((MemberExpression)propertyExpression.Body).Member;
        var declaringType = memberInfo.DeclaringType;

        Console.WriteLine(declaringType.Name); // outputs "MyClass"

        return false;
    }
}
max
  • 33,369
  • 7
  • 73
  • 84
0

Link reference here:

It work good with syntax:

PermissHelper.CheckPermiss(()=>MenuPermiss.PermissDelete) 

I try implement with this code, and it's return "<>c__DisplayClass0" value. How to fix it?

PermissHelper.CheckPermiss(MenuPermiss.PermissDelete) 

and

public static string CheckPermiss(PermissInfo permissInfo)
{
    Expression<Func<PermissInfo>> x = () => permissInfo;

    var memberInfo = ((MemberExpression)x.Body).Member;
    var declaringType = memberInfo.DeclaringType;

    return declaringType != null ? declaringType.Name : "";
}
Community
  • 1
  • 1
minhhungit
  • 180
  • 4
  • 25