gpt4 book ai didi

c# - C# 中的复杂函数和类型委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 17:38:19 26 4
gpt4 key购买 nike

我是一名新手/中级 C# 开发人员,正在尝试完成一项我会在 C 中轻松完成的任务。

基本上,我收到了 XML 请求,我需要用相应的响应来响应这些请求。请求/响应对在如下结构中明确定义:

  • “Method1Request”收到“Method1Response”
  • “Method2Request”收到“Method2Response”
  • ...

因此,我将在名为“Method1”、“Method2”...的类中创建函数,这些函数采用参数“Method1Request”、“Method2Request”...并返回“Method1Response”、“Method2Response” ,...

在代码中,函数声明看起来像这样,伴随的代码是多种多样的:

  • Mtehod1Response Method1(Method1Request);
  • Method2Response Method2(Method2Request);
  • ...

所有的函数响应都会立即被序列化为XML,所以,理论上,函数都可以这样,并且可以将结果写入私有(private)字符串变量“response”:void Method1(Method1Request)void Method2(Method2Request)...

无论如何,我想做的是“采样”XML(已经可以正常工作),找出它是什么类型的请求,并使用 case 语句从本质上对请求运行适当的函数, ,因此,生成正确的响应。

因此,我需要创建一个函数来执行所有在调用这些特定函数时保持不变的步骤。它的结构基本上是这样的(!伪代码!):

void ProcessRequestResponse(Type RequestType, Type ResponseType, Method MethodToRun)
{
...create serializers for RequestType and ResponseType...
...deserialize XML into RequestType...
[ResponseType] [MethodToRun]([RequestType]);
...serialize ResponseType into XML...
...store XML as variable in class...
}

我只是不知道如何实际创建该函数并让它知道该函数与哪些类型一起运行。我研究了使用委托(delegate)“Func”方法,但我没有看到用创建时未知的类型来定义它们的方法。

任何有关如何解决此问题的指导都将不胜感激。我认为我不需要为我的 15 多个案例中的每一个都键入所有代码(希望如此!)并管理几乎相同代码的几乎相同的实例。

最佳答案

我会走泛型路线,实现一些辅助类并使用配置对象(可以是简单的字典)解析请求处理策略。

如果您有 15 种以上的请求类型,我更愿意有 15 种以上的替代类,每个类都实现相应的处理策略。

警告:只是一些一次性的示例代码,但应该让您有一个良好的开端。

// Define a weakly typed interface, to be called
// by the invoking code.
public interface IRequestHandler
{
public string HandleRequest(string xmlRequest);
}

// Defines a generic handler, accepting two type parameters, one
// for the request, one for the response.
public abstract class RequestHandler<RequestType, ResponseType> : IRequestHandler
{

public XmlSerializer GetRequestSerializer()
{
return GetSerializer(typeof(RequestType));
}

public XmlSerializer GetResponseSerializer()
{
return GetSerializer(typeof(ResponseType));
// an alternative, depending upon your deserialization library,
// could be:
// return GetSerializer<ResponseType>();
}

public XmlSerializer GetSerializer(Type dataType)
{
... resolve based on type.
}

public string HandleRequest(string xmlRequest)
{
if (request == null) throw new ArgumentNullException("request");

var requestSerializer = GetRequestSerializer();
var typedRequest = requestSerializer.Deserialize(xmlRequest) as RequestType;
// response is a ResponseType
var response = ProcessRequest(typedRequest);

var responseSerializer = GetResponseSerializer();

return responseSerializer.Serialize(response);
}

protected abstract ResponseType ProcessRequest(RequestType request);
}

// One handler implementation
// you can just declare the class and RequestHandler inheritance,
// and then right click and ask Visual Studio to "Implement abstract members"
public class ActualRequestHandler : RequestHandler<ActualRequest, ActualResponse>
{
protected ActualResponse ProcessRequest(ActualRequest request)
{
// ... do your processing
}
}

// One handler implementation
public class AnotherRequestHandler : RequestHandler<AnotherRequest, AnotherResponse>
{
protected AnotherResponse ProcessRequest(AnotherRequest request)
{
// ... do your processing
}
}

一个简单的配置可以是外部处理器类中的静态字典:

private static readonly Dictionary<Type, Func<IRequestHandler>> _strategyGetter = new Dictionary<Type, IRequestHandler>()
{
{typeof(ActualRequest), () => new ActualRequestHandler()},
{typeof(AnotherRequest), () => new AnotherRequestHandler()}
};

最后,您的全局代码:

Type requestType = ... your existing code processing the string request ...
Func<IRequestHandler> handlerFactoryMethod;
if(_strategyGetters.TryGetValue(requestType, out handlerFactoryMethod))
{
var handler = handlerFactoryMethod();
var responseString = handler(request);

// ... do what you want with responseString ...
}
else
{
// seems the request was not recognized.
}

关于c# - C# 中的复杂函数和类型委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799601/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com