- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是一名新手/中级 C# 开发人员,正在尝试完成一项我会在 C 中轻松完成的任务。
基本上,我收到了 XML 请求,我需要用相应的响应来响应这些请求。请求/响应对在如下结构中明确定义:
因此,我将在名为“Method1”、“Method2”...的类中创建函数,这些函数采用参数“Method1Request”、“Method2Request”...并返回“Method1Response”、“Method2Response” ,...
在代码中,函数声明看起来像这样,伴随的代码是多种多样的:
所有的函数响应都会立即被序列化为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/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!