C# 如何通过 SignalR 传递复杂对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17350582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass complex objects via SignalR?
提问by AngryHacker
There is an excellent tutorial on SignalRthat explains how to pass .NET objects as parameters to Javascript and vice versa. In that case it passes a ChatMessageobject to and from.
SignalR上有一个很好的教程,解释了如何将 .NET 对象作为参数传递给 Javascript,反之亦然。在这种情况下,它传入ChatMessage和传出一个对象。
However, the tutorial addresses a really simple object. I'd like to see how to deal with complex .NET objects (that have other objects as properties) in Javascript.
然而,本教程解决了一个非常简单的对象。我想看看如何在 Javascript 中处理复杂的 .NET 对象(具有其他对象作为属性)。
For instance, consider the following object:
例如,考虑以下对象:
class Master {
public List<QuarterHour> QuarterHours { get; set; }
public List<string> Books { get; set; }
public int EndDay { get; set; }
public int StartDay { get; set; }
}
class QuarterHour {
public MinuteInstance Minute {get; set;}
public int HourStart { get; set;}
}
class MinuteInstance {
public bool Registered {get; set;}
public int NumAttendees {get; set;}
}
In .NET, I can reference a value like this: master.QuarterHours[2].Minute.Registered. My questions:
在 .NET 中,我可以引用这样的值:master.QuarterHours[2].Minute.Registered. 我的问题:
- How would I do reference
master.QuarterHours[2].Minute.Registeredin the receiver method on the Javascript end? - How would I build the
Masterobject in Javascript to be sent to the .NET end?
- 我将如何
master.QuarterHours[2].Minute.Registered在 Javascript 端的接收器方法中进行引用? - 我将如何
Master在 Javascript 中构建要发送到 .NET 端的对象?
采纳答案by N. Taylor Mullen
- You just send it and reference it the same way.
- You'd pass (this is how it looks when you get it from the server):
- 您只需发送它并以相同的方式引用它。
- 你会通过(这是从服务器获取它时的样子):
{ QuarterHours: [{ Minute: { Registered: true, NumAttendees: 1337 }, HourStart: 1 }], Books: ["Game of Thrones", "Harry Potter"], EndDay: 2, StartDay: 3 }
{ QuarterHours: [{ Minute: { Registered: true, NumAttendees: 1337 }, HourStart: 1 }], Books: ["Game of Thrones", "Harry Potter"], EndDay: 2, StartDay: 3 }
回答by SeanPrice
You would want to serialize your class into a JSON object. There are many ways to accomplish this, but you can try JSON.NETto do it quick and easy.
您可能希望将您的类序列化为 JSON 对象。有很多方法可以完成此操作,但您可以尝试使用JSON.NET来快速轻松地完成此操作。
If its not already included in your project, you can add this through Nuget with:
如果它尚未包含在您的项目中,您可以通过 Nuget 添加它:
Install-Package Newtonsoft.Json
The code would look something like:
代码如下所示:
var json = JsonConvert.SerializeObject(master);
Once this is passed to your client-side, you can then read from your JSON object like any other. You can use the following javascript code to convert your SignalR string message to a JSON object:
一旦将其传递给您的客户端,您就可以像其他任何对象一样从您的 JSON 对象中读取数据。您可以使用以下 javascript 代码将 SignalR 字符串消息转换为 JSON 对象:
var master = JSON.stringify(eval("(" + message + ")"));
var registered = master.QuarterHours[2].Minute.Registered;
You can also pass this through SignalR to the server and deserialize the JSON object using JsonConvert.DeserializeObject in order to convert it to your C# classes. Check out the documentation here for further details: http://james.newtonking.com/projects/json/help/
您还可以通过 SignalR 将其传递到服务器并使用 JsonConvert.DeserializeObject 反序列化 JSON 对象,以便将其转换为您的 C# 类。查看此处的文档以获取更多详细信息:http: //james.newtonking.com/projects/json/help/

