创建可序列化匿名委托的替代方案
有很多关于此的帖子,都试图序列化 Func 委托。
但是,当委托的使用总是很明确时,有人可以想到替代方案吗?
我们有一个通用的创建命令,它在构造函数中将委托作为参数。该委托将为创建命令创建项目:
public class CreateCommand<T> : Command
{
public T Item;
protected Func<T> Constructor;
public ClientCreateCommand(Func<T> constructor)
{
Constructor = constructor;
}
public override void Execute()
{
Item = Constructor();
}
}
该命令的使用方式如下:
var c = new CreateCommand<MyType>( () => Factory.CreateMyType(param1, param2, ...) );
History.Insert(c);
然后历史记录序列化该命令并将其发送到服务器。 ofc 委托不能按原样序列化,我们得到一个异常。
现在有人可以想到一个非常简单的构造函数类,它可以序列化并且与 lambda 表达式执行相同的工作吗?意味着它需要一个参数列表并返回一个 T 类型的实例,然后我们可以这样写:
var constructor = new Constructor<MyType>(param1, param2, ...);
var c = new CreateCommand<MyType>(constructor);
History.Insert(c);
Constructor 类会是什么样子?感谢您的任何想法!
there have been quite some posts about this, all trying to serialize a Func delegate.
But could someone think of an alternative, when the use of the delegate is always clear?
We have a generic create command, which takes a delegate as paramater in the constructor. This delegate will create the Item for the create command:
public class CreateCommand<T> : Command
{
public T Item;
protected Func<T> Constructor;
public ClientCreateCommand(Func<T> constructor)
{
Constructor = constructor;
}
public override void Execute()
{
Item = Constructor();
}
}
The command is used like this:
var c = new CreateCommand<MyType>( () => Factory.CreateMyType(param1, param2, ...) );
History.Insert(c);
Then the History serializes the command and sends it to the server. ofc the delegate can't be serialized as is and we get an exception.
Now could someone think of a very simple Constructor class that can be serialized and does the same job than the lambda expresseion? Means it takes a list of paramters and returns an instance of type T, that we then can write somethink like this:
var constructor = new Constructor<MyType>(param1, param2, ...);
var c = new CreateCommand<MyType>(constructor);
History.Insert(c);
How would the Constructor class look like? Thanks for any ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑(2):我提供了几个完整的示例实现。它们分为以下几类:“实施 1”和“实施 2”。
你的代表本质上是一个工厂。您可以定义一个工厂接口并创建一个为您的 Item 类实现该接口的类。下面是一个示例:
您可以按以下方式使用此代码:
编辑(1):您的应用程序需要的
IFactory
的具体实现将由您决定。您可以为您需要的每个类创建特定的工厂类,或者您可以创建某种工厂来动态创建实例,例如使用 Activator.CreateInstance 函数或可能使用某种控制反转框架(例如 Spring 或结构图。下面是使用两个工厂实现的完整示例实现。一种实现可以使用带有匹配参数的类型的构造函数来创建给定参数数组的任何类型。另一个实现创建已在我的“Factory”类中注册的任何类型。
Debug.Assert 语句确保一切都按预期运行。我运行这个应用程序没有错误。
实现 1
编辑(2):重新阅读问题后,我想我更好地了解了提问者真正想要了解的内容。本质上,我们仍然希望利用 lambda 表达式/匿名委托提供的灵活性,但避免序列化问题。
下面是另一个示例实现,它利用
Factory
类来存储用于返回 T 类型实例的委托。实现 2
EDIT(2): I've provided a couple of complete example implementations. They are categorized below as "Implementation 1" and "Implementation 2".
Your delegate is essentially a factory. You could define a factory interface and create a class that implements that interface for your Item class. Below is an example:
You would utilize this code in the following manner:
EDIT(1): The specific implementations of
IFactory<T>
that your application needs will be up to you. You could create specific factory classes for each class that you need, or you could create some kind of factory that dynamically creates an instance using, for example, the Activator.CreateInstance function or perhaps using some kind of Inversion of Control framework such as Spring or StructureMap.Below is a complete example implementation that uses two factory implementations. One implementation can create any type given an array of arguments using that type's constructor with matching parameters. Another implementation creates any type that has been registered with my "Factory" class.
The Debug.Assert statements ensure that everything is behaving as intended. I ran this application without error.
Implementation 1
EDIT(2): After re-reading the question, I think I got a better idea of what the asker was really trying to get at. Essentially, we still want to take advantage of the flexibility offered by lambda expressions / anonymous delegates, but avoid the serialization issues.
Below is another example implementation that utilizes a
Factory<T>
class to store delegates used to return instances of type T.Implementation 2