How can I override get and set methods for all properties in a class?

I have several classes similar to the ones below, and I need to do some checks in the get method and custom set methods. Adding code to each get and set method makes everything really confusing.

Is there a way to override the get and set methods for all properties in the entire class?

public class Test
{
    private DataRow _dr;
    public Test()
    {
        _dr = GetData();
    }

    public string Name
    {
        get { return _dr[MethodBase.GetCurrentMethod().Name.Substring(4)].ToString(); }
        set
        {
            VerifyAccess(MethodBase.GetCurrentMethod().Name.Substring(4), this.GetType().Name);
            _dr[MethodBase.GetCurrentMethod().Name.Substring(4)] = value;
        }
    }

    public string Description
    {
        get { return _dr[MethodBase.GetCurrentMethod().Name.Substring(4)].ToString(); }
        set
        {
            VerifyAccess(MethodBase.GetCurrentMethod().Name.Substring(4), this.GetType().Name);
            _dr[MethodBase.GetCurrentMethod().Name.Substring(4)] = value;
        }
    }

    public string DescriptionUrl
    {
        get { return _dr[MethodBase.GetCurrentMethod().Name.Substring(4)].ToString(); }
        set
        {
            VerifyAccess(MethodBase.GetCurrentMethod().Name.Substring(4), this.GetType().Name);
            _dr[MethodBase.GetCurrentMethod().Name.Substring(4)]= value;
        }
    }


    private void VerifyAccess(string propertyname, string classname)
    {
        //some code to verify that the current user has access to update the property
        //Throw exception
    }

    private DataRow GetData()
    {
        //Some code to pull the data from the database
    }
}
+3
source share
6 answers

I think you need Proxyfor your class, read about Proxy PatternandDynamic Proxies

+1
source

Not directly, there is no way to do this using the compiler only. You will need to generate your entire binary file and then process it using an external tool.

; , .

+1

. - ( ), . - . , ( , ... .. get , x). ( - ), / , .

, Mono-Cecil .

( , IL-),

+1

get/set, . , , / .

Smth :

public string Name
{
    get { return GetProperty(MethodBase.GetCurrentMethod()); }
    set
    {
        SetProperty(MethodBase.GetCurrentMethod(), value);
    }
}

private string GetProperty(MethodBase method)
{
    return _dr[method.Name.Substring(4)].ToString();
}

private void SetProperty(MethodBase method, string value)
{
    string methodName = method.Name.Substring(4);
    VerifyAccess(methodName , this.GetType().Name);
    _dr[methodName] = value;
}
0

, . obj.PropA.Value = obj.PropB.Value + 1 - . , .

// attribute -- bind later in central spot with annotation application
[MyCustomProp(4)] CustProp<int> Age;

// direct -- explicit binding, could also post-process dynamically
CustProp<int> Age = new CustProp<int>(4, this);

, , , TT4, .

"KISS": -)

0

, - .

Now I'm looking for an answer ... The best idea I had was to define all the properties you want to check as a generic class. For instance:

public class Foo {
    public String Name { 
        get{ return _Name.value; }
        set{ _Name.value = value; }
    }
    private Proxy<String> _Name;

    static void main(String[] args) {
        Foo f = new Foo();

        //will go through the logic in Proxy.
        f.Name = "test"; 
        String s = f.Name;
    }
}
public class Proxy<T> {
    public T value { 
        get { 
            //logic here
            return _this; 
        } set {
            //logic here
            _this = value;
        } 
    }
    private T _this;
}
0
source

Source: https://habr.com/ru/post/1783571/


All Articles