从字符串转换为称为事件的自定义复杂对象 [英] Casting from a String to a custom complex object called Event
问题描述
我到处都在寻找答案.我正在将自定义类/对象序列化并将其转换为可以传输到Web服务器的格式. Web服务器对响应进行加密,然后通过网络将加密的序列化响应返回给应用程序.
Hi,
I have looked everywhere for an answer to this. I am serializing and converting a custom class/object to a format that can be transferred to a web server. The web server encrypts the response and returns the encrypted serialized response to the application via the Net.
Event ev = new Event();//Custom complex object that is serializable.
String EventToSend = Convert.ToBase64String(Serialize(ev));
EventToSend = Encryption.Encrypt(EventToSend);
Event ReceivedEvent = (Event)Encryption.Decrypt(EventToSend);
//Decrypt to Event object.
服务器在以下代码行中进行仿真:
The server is emulated in the line of code:
EventToSend = Encryption.Encrypt(EventToSend);
Encryption.Decrypt()返回一个对象.
我的问题是从对象到事件的转换.如何将返回的对象强制转换或转换为我的事件?
我已经尝试过"as"演员(以及以上),但没有任何运气.我不明白如何将对象(字符串)转换为事件.
感谢您的帮助.
Encryption.Decrypt() returns an object.
My issue is the cast from the object to my Event. How can I cast or convert the returned object to my Event?
I have tried "as" cast (along with the above) without any luck. I don''t understand how I could cast from an object (that was a string) to my Event.
Thanks for any help.
推荐答案
String EventToSend = Convert.ToBase64String(Serialize(ev));
如果要在出局时序列化它,则在...中对它进行反序列化.
传出程序(您已经在执行此操作):
1)序列化您的Event对象.
2)加密序列化的数据.
3)发送加密的数据.
因此,传入过程必须为:
1)接收数据.
2)解密数据.
3)反序列化为Event对象.
这里有一篇介绍序列化的文章:使用C#进行对象序列化 [
If you are serializing it on the way out, then deserialize it on the way in...
Outgoing procedure (you are doing this already):
1) Serialize your Event object.
2) Encrypt the serialized data.
3) Send the encrypted data.
So the incoming procedure must be:
1) Receive data.
2) Decrypt data.
3) Deserialize to Event object.
There is an article that explains serialization here: Object Serialization using C#[^]
BTW: It might be worth changing the name of your complex object: event
already exists, and having such a similar name for your objects is open to errors!
这篇关于从字符串转换为称为事件的自定义复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!