c#中如何通过引用传递属性? [英] How to pass properties by reference in c#?

查看:29
本文介绍了c#中如何通过引用传递属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确地问了这个问题.如果我错了,请纠正我.

I am not sure if I have asked the question correctly. Please correct me if I am wrong.

无论如何,我们希望在页面生命周期的不同阶段使用变量的值.

Anyways, we would like to use a variable's value on a different phases of the page's life cycle.

例如,

public partial class TestUserControl: UserControl{
    public TestUserControl(){
        Objects = new List<object>(){
            Property1,
            Property2,
            Property3
        };
    }

    public int Property1 { get; set; }
    public bool Property2 { get; set; }
    public string Property3 { get; set; }
    public List<object> Objects { get; set; }

    protected override OnLoad(EventArgs e){
        foreach(var item in Objects){
            Page.Controls.Add(new LiteralControl(item.ToString() + "<br/>"));
        }
    }
}

所以如果我们说 Property1、Property2、Property3 的值是按照标签上写的那样设置的,我们怎么能在需要的时候使用这些属性的值?

So if we say the values of Property1, Property2, Property3 are set as they were written on the tag, how could we use the properties' values just as when needed?

在构造函数中,不会将属性的值列在列表中,只会列出属性的名称,因此它们的当前值将用于 OnLoad.

That on the constructor, instead of the values of the properties will be listed down on the List, only the Properties' names will be listed down so their current values will be used on OnLoad.

非常感谢.


我采用了 deerchao 和 Jon 的方法,但似乎我们将面临另一个问题......就像:


I have adopted deerchao's and Jon's approach, but there seems to be another problem we will be facing... It's like:

public partial class TestUserControl: UserControl{
    public TestUserControl(){
        Objects = List<Func<object>>{
            () => Property1,
            () => Property2,
            () => Property3
        };
    }

    public int Property1 { get; set; }
    public bool Property2 { get; set; }
    public string Property3 { get; set; }
    public List<Func<object>> Objects { get; set; }

    protected override OnLoad(EventArgs e){
        foreach (var item in Objects) {
            //Here is the problem... can we get the variable name using this approach?
            //We have tried some other ways like reflection and expressions but to no avail. Help please =)
            string key = GetVariableName(item());

            string value = item() == null ? string.Empty : item().ToString();
            Page.Controls.Add(new LiteralControl(key + " : " + value + "<br/>"));
        }
    }
}

推荐答案

您可以通过引用传递变量(使用ref)但不能通过属性.此外,ref-ness"仅适用于方法的持续时间:它为参数和参数设置别名,但如果您将参数值复制到另一个变量中,则该另一个变量不是别名.

You can pass variables by reference (using ref) but not properties. Additionally, the "ref-ness" is only for the duration of the method: it aliases the argument and the parameter, but if you copy the parameter value into another variable, that other variable isn't an alias.

这里并不清楚您到底想做什么,但您可以使用委托:

It's not really clear exactly what you want to do here, but you could use delegates instead:

    Objects = new List<Func<object>>(){
        () => Property1,
        () => Property2,
        () => Property3
    };

那么:

protected override OnLoad(EventArgs e){
    foreach(var itemRetriever in Objects){
        Page.Controls.Add(new Literal(itemRetriever().ToString() + "<br/>"));
    }
}

虽然很讨厌 - 如果可以,我会尝试找到替代设计.

It's pretty nasty though - I'd try to find an alternative design if you can.

这篇关于c#中如何通过引用传递属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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