我正在做装配分析项目,遇到了问题。
我想要实现的是一个类实现的所有接口的列表,但没有派生接口(以及派生类实现的接口)。
下面是一个示例来说明(来自 LinqPad,.Dump()是一个打印到结果窗口):
void Main()
{
typeof(A).GetInterfaces().Dump(); //typeof(IT), typeof(IT<Int32>)
typeof(B).GetInterfaces().Dump(); //typeof(IT<Int32>)
typeof(C).GetInterfaces().Dump(); //typeof(IT), typeof(IT<Int32>)
}
class C : A {}
class A : IT {}
class B : IT<int> {}
public interface IT : IT <int> {}
public interface IT<T> {}
Run Code Online (Sandbox Code Playgroud)
我想得到的是
typeof(A).GetInterfaces().Dump(); //typeof(IT)
typeof(B).GetInterfaces().Dump(); //typeof(IT<Int32>)
typeof(C).GetInterfaces().Dump(); //
Run Code Online (Sandbox Code Playgroud)
我发现这篇文章Type.GetInterfaces() 对于声明的接口仅带有答案
Type type = typeof(E);
var interfaces = type.GetInterfaces()
.Where(i => type.GetInterfaceMap(i).TargetMethods.Any(m => m.DeclaringType == type))
.ToList();
Run Code Online (Sandbox Code Playgroud)
但我正在寻找是否有一种替代方法可以迭代方法。
有什么办法可以实现这一点吗?
我目前正在使用 Azure API 管理中的策略,并且对提取从响应正文返回的值感兴趣。
<send-one-way-request mode="new">
<set-url>http://requestb.in/xje199xj</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
//JObject or string?
string response = context.Response.Body.As<string>(preserveContent: true);
//something here..
}
</set-body>
</send-one-way-request>
Run Code Online (Sandbox Code Playgroud)
响应如下所示:
"getBookableResourcesResponse": {
"getBookableResourcesResult": {
"hasError": false,
"errorCode": 0,
"BookableResource": [
{
"resourceCode": "TRA",
"description": "Trailer",
"group": "F",
"subGroup": "C",
"category": "R",
"dialogType": "CARGO",
"orgCode": "DECK",
"length": 14,
"taraWeight": "7000",
"grossWeight": "25001",
"AddResource": [
{
"resourceCode": "EXPFIN",
"description": "Export Finland",
"dialogType": "UNDEFINED",
"amount": "0",
"ticketType": "",
"orgCode": "EXPFIN",
"required": "false"
}.....`
Run Code Online (Sandbox Code Playgroud)
我想要获取从“resourceCode”属性返回的值,在本例中为“TRA”,然后创建一个新的 JObject,我可以将其发送到我的 …