以下代码可以正常工作:
public static void Main()
{
Foo<int>(5);
}
private static void Foo<T>(T x)
{
Bar((int)(object)x);
}
private static void Bar(int x)
{
}
但是,我的Bar方法来自第三方库(Json.NET),该库实现了多个重载。示例:
private static void Bar(string x)
{
}
如果我调用Foo<int>(5)
,则Bar((int)(object)x)
转换可以正常工作,但是调用Foo<string>("")
会在运行时中断(由于明显的原因,您不能将字符串转换为int)。
因此,我想将Bar((int)(object)x)
转换更改为通用Bar((T)(object)x)
转换,但这会导致以下编译错误:
无法将“ T”转换为“ int”
那么,是否有可能将对象转换为T或唯一的解决方案是使用切换大小写转换?
答案 0 :(得分:0)
该错误实际上意味着编译器不知道要调用GET : drives/[[REDACTED]]/root/search(q='testfile.txt')
//HEADERS
Content-Type = application/json; charset=utf-8
Authorization = REDACTED
//BODY
{
"$select":"id,name,file"
}
//END
RESPONSE 200 : 671ms
//HEADERS
Cache-Control = private
request-id = 96015ad0-3a5b-46aa-80f4-f6d6d2c903ea
Status_Code = 200
Http_Version = HTTP/1.1
Explanation = OK
Strict-Transport-Security = max-age=31536000
Connection = close
Transfer-Encoding = chunked
Date = Thu, 04 Apr 2019 08:30:11 GMT
Content-Type = application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
Vary = Accept-Encoding
OData-Version = 4.0
x-ms-ags-diagnostic = {"ServerInfo":{"DataCenter":"UK South","Slice":"SliceC","Ring":"5","ScaleUnit":"002","RoleInstance":"AGSFE_IN_1","ADSiteName":"UKS"}}
client-request-id = 96015ad0-3a5b-46aa-80f4-f6d6d2c903ea
Duration = 631.8379
//BODY
{
"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#Collection(driveItem)",
"@odata.nextLink":"https://graph.microsoft.com/v1.0/drives/[[REDACTED]]/root/search(q='testfile.txt')?$select=id%2cname%2cfile&$skiptoken=[[REDACTED]]",
"value":[
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Other"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Sample file"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Business Contacts"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Training"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"testfolders"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"DD"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Club"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Calendar 2019.pdf",
"file":{
"mimeType":"application/pdf"
}
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Quick Start Guide.pdf",
"file":{
"mimeType":"application/pdf"
}
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Onboarding.pdf",
"file":{
"mimeType":"application/pdf"
}
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Offices"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 1"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Data"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Sprints"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Meeting Minutes"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Reports"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"IT"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"General Documentation"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Marketing"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Video demos"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Training"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Others"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Scripts"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"testfolder"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Emma"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Offices"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Offices"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Update September 2018.pdf",
"file":{
"mimeType":"application/pdf"
}
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 2"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 3"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 4"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 5"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Office 6"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Meeting Minutes.pdf",
"file":{
"mimeType":"application/pdf"
}
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Release Notes for Users"
},
{
"@odata.type":"#microsoft.graph.driveItem",
"id":"[[REDACTED]]",
"name":"Presentation"
}
And so on ...
]
}
//END
的哪个重载,因为Bar
在编译时可以是任何类型。
您需要在运行时检查T
是T
还是int
并显式转换为所需的类型。
string