在基类型" virtual
"中声明方法然后使用" override
"关键字在子类型中覆盖它而不是new
在声明子类型中的匹配方法时简单地使用" "关键字之间有什么区别?
我有一个可以为空的int类?数据类型设置为序列化为xml元素.有没有办法设置它,所以如果值为null,xml序列化程序将不会序列化该元素?
我试图添加[System.Xml.Serialization.XmlElement(IsNullable = false)]属性,但我得到一个运行时序列化异常,说有一个反映类型的错误,因为"IsNullable可能不会设置为'false '对于Nullable类型.考虑使用'System.Int32'类型或从XmlElement属性中删除IsNullable属性."
[Serializable]
[System.Xml.Serialization.XmlRoot("Score", Namespace = "http://mycomp.com/test/score/v1")]
public class Score
{
private int? iID_m;
...
/// <summary>
///
/// </summary>
public int? ID
{
get
{
return iID_m;
}
set
{
iID_m = value;
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
上面的类将序列化为:
<Score xmlns="http://mycomp.com/test/score/v1">
<ID xsi:nil="true" />
</Score>
Run Code Online (Sandbox Code Playgroud)
但对于null的ID,我根本不需要ID元素,主要是因为当我在MSSQL中使用OPENXML时,对于看起来像的元素,它返回0而不是null
我正在尝试向文本框添加自动完成功能,结果来自数据库.它们的格式为
[001]最后,第一中间
目前,您必须输入[001] ...才能显示要显示的条目.所以问题是我希望它完成,即使我先键入firstname.所以,如果一个条目是
[001] Smith,John D.
如果我开始输入John,则此条目应显示在自动完成的结果中.
目前代码看起来像
AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
txtBox1.AutoCompleteCustomSource = acsc;
txtBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
txtBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
....
if (results.Rows.Count > 0)
for (int i = 0; i < results.Rows.Count && i < 10; i++)
{
row = results.Rows[i];
acsc.Add(row["Details"].ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
results是包含查询结果的数据集
查询是使用like语句的简单搜索查询.如果我们不使用自动完成并将结果抛入数组,则会返回正确的结果.
有什么建议?
编辑:
这是返回结果的查询
SELECT Name from view_customers where Details LIKE '{0}'
Run Code Online (Sandbox Code Playgroud)
{0}是搜索字符串的占位符.
我有这个 json
{"id":"48e86841-f62c-42c9-ae20-b54ba8c35d6d"}
Run Code Online (Sandbox Code Playgroud)
我如何48e86841-f62c-42c9-ae20-b54ba8c35d6d
摆脱它?我能找到的所有例子都显示做类似的事情
var o = System.Text.Json.JsonSerializer.Deserialize<some-type>(json);
o.id // <- here's the ID!
Run Code Online (Sandbox Code Playgroud)
但是我没有符合这个定义的类型,我不想创建一个。我试过反序列化为动态,但我无法让它工作。
var result = System.Text.Json.JsonSerializer.Deserialize<dynamic>(json);
result.id // <-- An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Linq.Expressions.dll but was not handled in user code: ''System.Text.Json.JsonElement' does not contain a definition for 'id''
Run Code Online (Sandbox Code Playgroud)
任何人都可以提供任何建议吗?
编辑:
我刚刚发现我可以这样做:
Guid id = System.Text.Json.JsonDocument.Parse(json).RootElement.GetProperty("id").GetGuid();
Run Code Online (Sandbox Code Playgroud)
这确实有效 - 但有更好的方法吗?
我正在准备从ASP.NET Core 2.2迁移到3.0。
As I don't use any more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json
, I decided to see if I could drop the previous default Newtonsoft.Json
. Do note, I'm aware that System.Text.Json
will not completely replace Newtonsoft.Json
.
I managed to do that everywhere, e.g.
var obj = JsonSerializer.Parse<T>(jsonstring);
var jsonstring = JsonSerializer.ToString(obj);
Run Code Online (Sandbox Code Playgroud)
but in one place, where I populate an existing object. …
c# asp.net-core razor-pages asp.net-core-3.0 system.text.json
如何将通用JObject转换为camelCase普通json字符串?我尝试过使用JsonSerializerSettings但不起作用(Newtonsoft.Json 4.5.11)
[Test]
public void should_convert_to_camel_case()
{
var serializer = JsonSerializer.Create(new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
var jo = new JObject();
jo["CamelCase"] = 1;
var stringWriter = new StringWriter();
var writer = new JsonTextWriter(stringWriter);
serializer.Serialize(writer,jo);
var serialized = stringWriter.ToString();
Assert.AreEqual("{\"camelCase\":1}", serialized);
}
Run Code Online (Sandbox Code Playgroud)
更新 根据http://json.codeplex.com/workitem/23853无法完成(tnx到@nick_w的链接)
我试图用Json.Net序列化IPEndpoint对象,我收到以下错误:
从'System.Net.IPAddress'上的'ScopeId'获取值时出错.
导致错误的原因是我只使用端点中IPAddress对象的IPV4属性.当Json解析器尝试解析IPv6部分时,它会访问ScopeID属性,该属性会抛出套接字异常"对于引用的对象类型,不支持尝试的操作"(null将足以支持microsoft!)
我想知道除了撕掉一切并将地址信息编码为字符串之外是否还有其他解决方法?在某些时候,我确实想支持IPV6.如果IPAddress系列设置为Internetwork而不是InternetworkIPV6,是否有任何可以在Json.NET中完成忽略错误或只是不尝试序列化ScopeID?
谢谢,
Dinsdale
在 ASP.Net Core 2.2 中使用 JSON.Net 时,当序列化为 JSON 时,当其值为 null 时,我能够忽略属性:
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public DateTime? Created { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是,当使用内置于 JSON (System.Text.Json) 的新 ASP.Net Core 3.0 时,如果某个属性的值为 null,我找不到可以忽略该属性的等效属性。
我只能找到 JsonIgnore。
我错过了什么吗?
json json.net asp.net-core-3.0 .net-core-3.0 system.text.json
给定Win32窗口的句柄,我需要找到它相对于其父窗口的位置.
我知道几个函数(例如; GetWindowRect()
和GetClientRect()
),但它们都没有显式返回所需的坐标.
我该怎么做呢?
我DataContractJsonSerializer
用来将我的自定义对象序列化为JSON.但我想跳过其值为的数据成员null
.如果DataMember
是null
该节点不应该在JSON字符串.
我怎样才能做到这一点?给我一个简单code snippet
的工作.
c# serialization datacontractserializer datacontractjsonserializer
c# ×8
json.net ×3
.net ×2
.net-core ×1
asp.net-core ×1
autocomplete ×1
json ×1
overriding ×1
position ×1
razor-pages ×1
syntax ×1
system.net ×1
win32gui ×1
winapi ×1
window ×1
winforms ×1
xml ×1