通过参照在C#通过性 [英] Passing properties by reference in C#

查看:146
本文介绍了通过参照在C#通过性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做做到以下几点:

 的GetString(
    inputString,
    REF Client.WorkPhone)私人无效的GetString(字符串inValue,楼盘串outValue)
{
    如果(!string.IsNullOrEmpty(inValue))
    {
        outValue = inValue;
    }
}

这是给我一个编译错误。我认为它的pretty清楚我想要实现的。基本上我想要的GetString 复制输入字符串客户端在办公电话属性的内容

是否有可能通过引用传递的属性?


解决方案

属性不能被引用传递。下面是可以解决此限制的几种方式。

1。返回值

 字符串的GetString(字符串输入,字符串输出)
{
    如果(!string.IsNullOrEmpty(输入))
    {
        返回输入;
    }
    返回输出;
}无效的主要()
{
    变种人=新的人();
    person.Name = GetString的(测试,person.Name);
    Debug.Assert的(person.Name ==测试);
}

2。代表

 无效的GetString(字符串输入,动作<串GT; setOutput)
{
    如果(!string.IsNullOrEmpty(输入))
    {
        setOutput(输入);
    }
}无效的主要()
{
    变种人=新的Person();
    GetString的(测试,值=> person.Name =值);
    Debug.Assert的(person.Name ==测试);
}

3。 LINQ防爆pression

 无效的GetString< T>(字符串输入,T目标,防爆pression< Func键< T,串>> outExpr)
{
    如果(!string.IsNullOrEmpty(输入))
    {
        VAR EXPR =(MemberEx pression)outExpr.Body;
        VAR道具=(的PropertyInfo)expr.Member;
        prop.SetValue(目标,输入,NULL);
    }
}无效的主要()
{
    变种人=新的Person();
    GetString的(测试,人,X => x.Name);
    Debug.Assert的(person.Name ==测试);
}

4。反射

 无效的GetString(字符串输入,目标对象,字符串propertyName的)
{
    如果(!string.IsNullOrEmpty(输入))
    {
        支撑= target.GetType()的getProperty(propertyName的)。
        prop.SetValue(目标,输入);
    }
}无效的主要()
{
    变种人=新的Person();
    GetString的(测试,人,姓名);
    Debug.Assert的(person.Name ==测试);
}

I'm trying to do do the following:

GetString(
    inputString,
    ref Client.WorkPhone)

private void GetString(string inValue, ref string outValue)
{
    if (!string.IsNullOrEmpty(inValue))
    {
        outValue = inValue;
    }
}

This is giving me a compile error. I think its pretty clear what I'm trying to achieve. Basically I want GetString to copy the contents of an input string to the WorkPhone property of Client.

Is it possible to pass a property by reference?

解决方案

Properties cannot be passed by reference. Here are a few ways you can work around this limitation.

1. Return Value

string GetString(string input, string output)
{
    if (!string.IsNullOrEmpty(input))
    {
        return input;
    }
    return output;
}

void Main()
{
    var person = new Person();
    person.Name = GetString("test", person.Name);
    Debug.Assert(person.Name == "test");
}

2. Delegate

void GetString(string input, Action<string> setOutput)
{
    if (!string.IsNullOrEmpty(input))
    {
        setOutput(input);
    }
}

void Main()
{
    var person = new Person();
    GetString("test", value => person.Name = value);
    Debug.Assert(person.Name == "test");
}

3. LINQ Expression

void GetString<T>(string input, T target, Expression<Func<T, string>> outExpr)
{
    if (!string.IsNullOrEmpty(input))
    {
        var expr = (MemberExpression) outExpr.Body;
        var prop = (PropertyInfo) expr.Member;
        prop.SetValue(target, input, null);
    }
}

void Main()
{
    var person = new Person();
    GetString("test", person, x => x.Name);
    Debug.Assert(person.Name == "test");
}

4. Reflection

void GetString(string input, object target, string propertyName)
{
    if (!string.IsNullOrEmpty(input))
    {
        prop = target.GetType().GetProperty(propertyName);
        prop.SetValue(target, input);
    }
}

void Main()
{
    var person = new Person();
    GetString("test", person, "Name");
    Debug.Assert(person.Name == "test");
}

这篇关于通过参照在C#通过性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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