进去setter属性名 [英] Get property name inside setter

查看:97
本文介绍了进去setter属性名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想preserve在ASP.Net应用程序回发之间的属性。目前,这样做:

 公众诠释MyIndex
{
    得到
    {
        返回(INT)会议[的ToString()+MyIndex];
    }
}
 

但将preFER是这样的:

 公众诠释MyIndex
{
    得到
    {
        返回(INT)会议[的ToString()+#code_that_returns_property_name#]。
    }
}
 

二传手忽略,但它只是使用相同的字符串值推入会议。 是否有某种方式使用反射,或不同的更好的解决方案?

解决方案

 公共静态INT假{
    得到 {
        变种propertyName的= MethodBase.GetCurrentMethod()Name.Substring(4)。
        Console.WriteLine(propertyName的);
        返回0;
    }
}
 

I want to preserve a property between postbacks in an ASP.Net application. Currently doing this:

public int MyIndex
{
    get
    {
        return (int)Session[ToString() + "MyIndex"];
    }
}

but would prefer something like:

public int MyIndex
{
    get
    {
        return (int)Session[ToString() + #code_that_returns_property_name#];
    }
}

Setter omitted, but it just pushes value into Session using the same string. Is there some way to use reflection, or a different better solution?

解决方案

public static int Dummy {
    get {
        var propertyName = MethodBase.GetCurrentMethod().Name.Substring(4);
        Console.WriteLine(propertyName);
        return 0;
    }
}

这篇关于进去setter属性名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆