有人可以解释一下吗?我在互联网上找不到任何东西,一切都在谈论如何以某种方式解决这个问题,但没有什么能说清楚到底是什么.
什么是完全可信的组件,它们如何彼此不同?
我有一个MS认证考试,这是我唯一不理解的话题.
编辑:谢谢你们.现在我对.NET的安全性有了更好的理解.我能够通过认证考试.
我有一个对象Foo,我将其序列化为XML流.
public class Foo {
// The application version, NOT the file version!
public string Version {get;set;}
public string Name {get;set;}
}
Foo foo = new Foo { Version = "1.0", Name = "Bar" };
XmlSerializer xmlSerializer = new XmlSerializer(foo.GetType());
Run Code Online (Sandbox Code Playgroud)
这可以快速,轻松地完成当前所需的一切.
我遇到的问题是我需要维护一个单独的文档文件,其中包含一些小的评论.如上例所示,Name
很明显,但是Version
应用程序版本而不是数据文件版本,正如人们在这种情况下所期望的那样.而且我还有许多类似的小事我想用评论来澄清.
我知道如果我使用该WriteComment()
函数手动创建我的XML文件,我可以这样做,但是我可以实现可能的属性或替代语法,以便我可以继续使用序列化程序功能吗?
我DataContractJsonSerializer
用来将我的自定义对象序列化为JSON.但我想跳过其值为的数据成员null
.如果DataMember
是null
该节点不应该在JSON字符串.
我怎样才能做到这一点?给我一个简单code snippet
的工作.
c# serialization datacontractserializer datacontractjsonserializer
我有以下简单的课程:
public abstract class GitObject
{
public Repository Repository { get; set; }
public abstract string Serialize();
public abstract void Deserialize(string data);
public class Blob : GitObject
{
public string Data { get; set; }
public Blob(Repository repository, string data = null)
{
if (data != null) Data = File.ReadAllText(data);
Repository = repository;
}
public override string Serialize()
{
return JsonSerializer.Serialize(this);
}
public override void Deserialize(string data)
{
Blob blobData = JsonSerializer.Deserialize<Blob>(data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道可能还有很大的改进空间(我很高兴听到这一点)。但是,该方法Deserialize
给了我错误
Each parameter …
Run Code Online (Sandbox Code Playgroud) 我的点击页面的Html代码是:
<input type="submit" id="publishButton-ns" class="ubtn ubtn-block"
name="publish" tabindex="10" value="Publish Post">
Run Code Online (Sandbox Code Playgroud)
我试过这个代码点击:
webBrowser1.Document.GetElementById("publishButton-ns").InvokeMember("click");
Run Code Online (Sandbox Code Playgroud)
但是没找到按钮.
我正在为 .NET Core 3.x 更新一些应用程序,作为其中的一部分,我正在尝试Json.NET
从新System.Text.Json
类迁移。使用 Json.NET,我可以反序列化一个匿名类型,如下所示:
var token = JsonConvert.DeserializeAnonymousType(jsonStr, new { token = "" }).token;
Run Code Online (Sandbox Code Playgroud)
新命名空间中是否有等效的方法?
任何人都可以在内存和引用方面告诉覆盖和隐藏的工作.
class A
{
public virtual void Test1() { //Impl 1}
public virtual void Test2() { //Impl 2}
}
class B : A
{
public override void Test1() { //Impl 3}
public new void Test2() { Impl 4}
}
static Main()
{
A aa=new B() //This will give memory to B
aa.Test1(); //What happens in terms of memory when this executes
aa.Test2(); //-----------------------SAME------------------------
}
Run Code Online (Sandbox Code Playgroud)
这里的内存是B类,但在第二个语句中,将调用aa.Test2类A的方法.为什么?如果B有内存,则应调用B的方法(在我看来).
任何非常深刻和完全描述这一基础的链接/练习将是一个很大的帮助.
I am trying to deserialize some JSON that contains a value that is sometimes an array, and sometimes a single item. How can I do this with System.Text.Json
and JsonSerializer
? (This question is inspired by this question for Json.NET by Robert McLaws.)
I have received the following JSON:
[
{
"email": "john.doe@sendgrid.com",
"timestamp": 1337966815,
"category": [
"newuser",
"transactional"
],
"event": "open"
},
{
"email": "jane.doe@sendgrid.com",
"timestamp": 1337966815,
"category": "olduser",
"event": "open"
}
]
Run Code Online (Sandbox Code Playgroud)
And I want to deserialize …
我正在尝试将 JObject 中的数组读取到 string[] 中,但我不知道怎么做。
代码非常简单,如下所示,但不起作用。Fails with error cannot convert JToken to string[]
JObject Items = jsonSerializer.Deserialize<JObject>(jtr);
string[] brands = null;
brands = (string[])Items.SelectToken("Documents[0].Brands");
Run Code Online (Sandbox Code Playgroud)
当我想阅读一个简单的 bool 时,以下内容有效,所以我知道我在正确的地方。
IsAdmin = (bool)Items.SelectToken("Documents[0].IsAdmin");
Run Code Online (Sandbox Code Playgroud)
Documents[0] 中的 JObject 如下所示
{
"id": "961AA6A6-F372-ZZZZ-1270-ZZZZZ",
"ApiKey": "SDKTest",
"ConsumerName": "SDKTest",
"IsAdmin": false,
"Brands": [
"ZZZ"
],
"IsSdkUser": true
}
Run Code Online (Sandbox Code Playgroud)
如何将 Brands 数组读入我的 string[] 品牌中?
我有一个对象树,我正在序列化为JSON DataContractJsonSerializer
.Dictionary<TKey, TValue>
得到序列化但我不喜欢标记 - 项目不会像这样呈现:
{key1:value, key2:value2}
Run Code Online (Sandbox Code Playgroud)
而是像一系列序列化KeyValuePair<TKey, TValue>
对象:
[{
"__type":"KeyValuePairOfstringanyType:#System.Collections.Generic",
"key":"key1",
"value":"value1"
},
{
"__type":"KeyValuePairOfstringanyType:#System.Collections.Generic",
"key":"key2",
"value":"value2"
}]
Run Code Online (Sandbox Code Playgroud)
丑,不是吗?
因此,我通过将通用Dictionary包装在实现的自定义对象中来避免这种情况ISerializable
,并且我在GetObjectData
方法中实现了自定义序列化(并且它只需要3行).
现在的问题-我不能让我的类从派生Dictionary<TKey, TValue>
,所以我实现所有的逻辑(Add
,Clear
,等),在我的自定义类,被应用到私人Dictionary<TKey, TValue>
领域.继承会更好,因为在使用我的自定义对象时,我将拥有所有通用字典功能.
继承的问题是它自己的Dictionary<TKey, TValue>
实现ISerializable
,并且DataContractJsonSerializer
似乎更喜欢这个实现,即使我ISerializable
从我的自定义类显式实现,如下所示:
public class MyClass : Dictionary<string, object>, ISerializable
{
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
}
Run Code Online (Sandbox Code Playgroud)
我真的很惊讶这是可能的,因为它允许我实现相同的接口两次而不显然能够使用显式接口实现 - 所以我在一篇关于多接口实现的博客文章中更详细地分析了这种情况
所以,根据我在那里做的实验,序列化器应该调用我的ISerializable实现,无论内部使用什么类型的转换 -
((ISerializable)((Dictionary<,>)obj)).GetObjectData(...)
Run Code Online (Sandbox Code Playgroud)
要么:
((ISerializable)obj).GetObjectData(...)
Run Code Online (Sandbox Code Playgroud)
但它显然没有发生,因为我在生成的JSON中看到KeyValuePair<TKey, TValue> …
.net oop serialization multiple-interface-implem datacontractjsonserializer