[英]C# Dynamic object has property with at symbol
I have an object from my elasticsearch resultset; 我的elasticsearch结果集中有一个对象; that I'm iterating true this via this foreach:
我通过这个foreach迭代了这个事实:
foreach (Nest.IHit<dynamic> temp in result.Hits) {
}
one temp
it's source looks like this (just rightclicked in visual studio and clicked on "copy value") 一个
temp
的来源如下所示(只需在Visual Studio中右键单击并单击“复制值”)
{{
"charSet": "UTF-8",
"executionTime": 927,
"parentUrl": "http://wfintranetdvlp.sidmar.be/sdg/",
"@timestamp": "2015-08-05T13:50:40.721Z",
"method": "GET",
"contentLength": 31575,
"mimeType": "text/html",
"text": "You are here: Home Productie Productie Productie Sidgal Sidgal 1 Campagneplan Dagverslag PamBrowser Sidgal 2 Campagneplan Dagverslag PamBrowser Sidgal 3 Campagneplan Dagverslag PamBrowser Alle Lijnen Stilstanden Productierapporten Autonoom Onderhoud JAP AO-zones Uitgevoerde AO-activiteit afgelopen jaar Kalender audits AO",
"title": "Productie",
"url": "http://wfintranetdvlp.sidmar.be/sdg/productie-2/",
"httpStatusCode": 200
}}
now in my code I can access the params like following temp.Source.title
or temp.Source.url
but when I want to access the @timestamp
it returns null
现在在我的代码中,我可以访问诸如
temp.Source.title
或temp.Source.url
类的参数,但是当我要访问@timestamp
它返回null
any idea on how I can access the timestamp? 关于如何访问时间戳的任何想法?
I deleted my original answer as I found this SO answer after trying something in my code for you. 我删除了我原来的答复,因为我发现这个苏答案试图在我的代码东西给你了。
The relevant code from the answer is this: 答案中的相关代码是这样的:
static object GetDynamicMember(object obj, string memberName)
{
var binder = Binder.GetMember(CSharpBinderFlags.None, memberName, obj.GetType(),
new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
var callsite = CallSite<Func<CallSite, object, object>>.Create(binder);
return callsite.Target(callsite, obj);
}
It uses reflection to build up the call to get the value back, and the "@timestamp" can easily be passed in as a string. 它使用反射来建立调用以取回值,并且“ @timestamp”可以轻松地作为字符串传递。
C# identifiers can not start with @
. C#标识符不能以
@
开头。 You're actually trying to access timestamp
- the @
is called the verbatim specifier, and it allows you to use keywords as identifiers, eg you can have a local named @this
, which is actually the this
identifier. 您实际上是在尝试访问
timestamp
- @
称为逐字说明符,它允许您将关键字用作标识符,例如,可以有一个本地名称@this
,实际上是this
标识符。
The only way would be to access the variable by name, something like yourvar["@timestamp"]
. 唯一的方法是按名称访问变量,例如
yourvar["@timestamp"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.