有没有办法在C#中的对象初始化程序块中使用扩展方法

Ant*_*ony 7 c# linq extension-methods initializer

下面的简单演示捕获了我想要做的事情.在真正的程序中,我必须使用对象初始化块,因为它正在读取LINQ to SQL选择表达式中的列表,并且有一个值我要读取数据库并存储在对象上,但是对象没有我可以为该值设置的简单属性.相反,它有一个XML数据存储.

看起来我无法在对象初始化块中调用扩展方法,并且我无法使用扩展方法附加属性.

我对这种方法运气不好吗?唯一的替代方案似乎是说服基类的所有者为这种情况修改它.

我有一个现有的解决方案,我将BaseDataObject子类化,但是这个问题也没有出现在这个简单的例子中.对象将作为BaseDataObject进行持久化和恢复 - 转换和测试将变得复杂.

public class BaseDataObject
{

    // internal data store
    private Dictionary<string, object> attachedData = new Dictionary<string, object>();

    public void SetData(string key, object value)
    {
        attachedData[key] = value;
    }

    public object GetData(string key)
    {
        return attachedData[key];
    }

    public int SomeValue { get; set; }
    public int SomeOtherValue { get; set; }

}

public static class Extensions
{
    public static void SetBarValue(this BaseDataObject dataObject,
                                        int            barValue)
    {
        /// Cannot attach a property to BaseDataObject?
        dataObject.SetData("bar", barValue);
    }
}

public class TestDemo
{

    public void CreateTest()
    {
        // this works
        BaseDataObject test1 = new BaseDataObject 
        { SomeValue = 3, SomeOtherValue = 4 };

        // this does not work - it does not compile
        // cannot use extension method in the initialiser block
        // cannot make an exension property  
        BaseDataObject test2 = new BaseDataObject { SomeValue = 3, SomeOtherValue = 4, SetBarValue(5) };
    }
}
Run Code Online (Sandbox Code Playgroud)

其中一个答案(来自mattlant)建议使用流畅的界面风格扩展方法.例如:

// fluent interface style
public static BaseDataObject SetBarValueWithReturn(this BaseDataObject dataObject, int barValue)
{
    dataObject.SetData("bar", barValue);
    return dataObject;
}

// this works
BaseDataObject test3 = (new BaseDataObject { SomeValue = 3, SomeOtherValue = 4 }).SetBarValueWithReturn(5);
Run Code Online (Sandbox Code Playgroud)

但这会在LINQ查询中起作用吗?

Tig*_*ine 5

对象初始化器只是语法糖,需要一个聪明的编译器,并且从当前实现开始,你不能在初始化器中调用方法.

var x = new BaseDataObject { SomeValue = 3, SomeOtherValue = 4 };
Run Code Online (Sandbox Code Playgroud)

会得到这样的编译器:

BaseDataObject tempObject = new BaseDataObject();
tempObject.SomeValue = 3;
tempObject.SomeOtherValue = 4;
BaseDataObject x = tempObject;
Run Code Online (Sandbox Code Playgroud)

不同之处在于不存在任何同步问题.变量x get一次分配了完全赋值的BaseDataObject,在初始化期间不能弄乱对象.

您可以在创建对象后调用扩展方法:

var x = new BaseDataObject { SomeValue = 3, SomeOtherValue = 4 };
x.SetBarValue()
Run Code Online (Sandbox Code Playgroud)

您可以将SetBarValue更改为具有可在初始化期间分配的get/set的属性:

public int BarValue
{
    set
    {
        //Value should be ignored
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以继承/使用facade模式将方法添加到对象:

public class DataObjectWithBarValue : BaseDataObject
{
    public void BarValue
    {
        set
        {
            SetData("bar", value);
        }
        get
        {
            (int) GetData("bar");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Pop*_*lin 3

更好的是:

public static T SetBarValue<T>(this T dataObject, int barValue)
        where T : BaseDataObject 
    {
        dataObject.SetData("bar", barValue);
        return dataObject;
    }
Run Code Online (Sandbox Code Playgroud)

您可以将此扩展方法用于 BaseDataObject 的派生类型,以链接方法而无需强制转换,并在推断为 var 字段或匿名类型时保留真实类型。