开发者

Storing a reference to an object to be

I don't know if this is possible at all so this is a shot in the dark.

Anyhow...

Consider having the following model:

    Class Model
    {
        public List<string> TheList = null;
    }

The List is set to null on purpose.

    var model = new Model();
    command.RegisterIn开发者_如何学CData( model => model.TheList ); // TheList is null at this point

    model.TheList = new List<string>();
    model.TheList.Add("A value here");

    command.Execute(); // <-- Here I want to access the new list somehow

As said, I don't know if anything like this is possible but I would like a push in the right direction.

The function desired: I would like to tell the command where to put the result before I have a concrete object.

Thanks in advance


This seems quite doable. Here is a variation with an even simpler accessor:

class Command
{
    private Func<List<string>> listAccessor;

    public void RegisterInData(Func<List<string>> listAccessor)
    {
        this.listAccessor = listAccessor;
    }

    public void Execute()
    {
        var list = this.listAccessor();
        foreach (string s in list)
        {
            Console.Log(s);
        }
    }
}

// Elsewhere
var model = new Model();
command.RegisterInData(() => model.TheList);

model.TheList = new List<string>();
model.TheList.Add("A value here");

command.Execute();

You'll probably want error handling for the case where RegisterInData is not called before Execute, but you get the idea.


You simply have to delay calling the delegate passed to RegisterInData and call it (I guess) at Execute.


Could Lazy be of use here? http://msdn.microsoft.com/en-us/library/dd642331.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜