相关疑难解决方法(0)

有没有办法让DataContractSerializer输出更清晰的XML?

使用DataContractSerializer序列化我的对象,我得到类似的输出

 <?xml version="1.0" encoding="utf-8" ?> 
 <AgentNotification xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/The.name.space.Notifications">
  <_x003C_Created_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_Id_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" />        
 <_x003C_Email_x003E_k__BackingField>some@email.com</_x003C_Email_x003E_k__BackingField> 
  <_x003C_Name_x003E_k__BackingField>Random Person</_x003C_Name_x003E_k__BackingField> 
 <_x003C_Policies_x003E_k__BackingField>
 <PolicyNotification>
  <_x003C_Created_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_Id_x003E_k__BackingField i:nil="true" xmlns="http://schemas.datacontract.org/2004/07/The.name.space" /> 
  <_x003C_ConfirmationNumber_x003E_k__BackingField>Some number</_x003C_ConfirmationNumber_x003E_k__BackingField>   
  </PolicyNotification>
 <PolicyNotification>
  </_x003C_Policies_x003E_k__BackingField>  
  </AgentNotification>
Run Code Online (Sandbox Code Playgroud)

有没有办法输出正好的标签

<Id>
<Name>
Run Code Online (Sandbox Code Playgroud)

等,不需要用属性覆盖我的类?

如果没有办法,每次正确的输出保证是相同的?因此,如果我使用它来渲染我的对象图是XML与文件生成的X*文档混搭,我将永远不会遇到我的节点更改名称并且文档空白正确的问题?

c# xml datacontractserializer

11
推荐指数
2
解决办法
6863
查看次数

没有 setter 的 Entity Framework Core 属性

在我的 .net core 3.1 应用程序中,我想将属性封装在某个实体中:

public class Sample : AuditableEntity
{
    public Sample(string name)
    {
        Name = name;
    }

    public int Id { get; }

    public string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)

因此,我删除了所有公共设置器,因此当我想检查此类 Sample 是否已存在时,我的代码中的某个位置

_context.Samples.Any(r => r.Name == name)
Run Code Online (Sandbox Code Playgroud)

该行导致错误:System.InvalidOperationException: 'No suitable constructor found for entity type 'Sample'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'name' in 'Sample(string name)'.'

所以我添加了代码空构造函数

public class Sample : AuditableEntity
{ …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework-core .net-core entity-framework-core-3.1

7
推荐指数
1
解决办法
6289
查看次数

如何设置没有setter的属性值

我已经看到了各种问题并回答了我们可以使用这样的反射来调用私有的setter:

是否有可能通过反思得到一个房产的私人制定者?

但是我有一些代码,它有一个我需要设置的属性但不能因为没有setter,我不能添加一个setter,因为这不是我的代码.在这种情况下,有没有办法以某种方式使用反射设置值?

c# reflection properties automatic-properties

6
推荐指数
2
解决办法
1万
查看次数

在C#中二进制反序列化期间更改类型

我们公司的解决方案之一消耗第三方服务.通过XML消息传递完成通信.在我们的最后,我们基于它们提供给我们的XML模式生成要使用的类,并且在某些时候我们将这些类型中的一些序列化为数据库中的二进制blob以供以后使用.

问题出现在第三方公司将其中一个字段从布尔值更改为整数类型的位置.现在,当我们尝试对已经存在的数据进行反序列化时,我们可以预见到会发生类型转换异常(无法从布尔值转换为整数).

我的问题是 - 我们如何使用旧的布尔类型反序列化我们数据库中的现有数据以将其转换为新的整数类型?

我已经尝试了很多东西 - 其中包括反思和实现ISerializable,但到目前为止还没有任何东西被淘汰.理想的解决方案是实现ISerializable,但是在尝试反序列化现有数据时遇到"未找到成员"错误,因为它已经仅使用Serializable属性进行了序列化.

欢迎任何建议!

编辑:添加一些代码以清楚地展示我的问题.

namespace ClassLibrary
{
    [Serializable]
    public class Foo //: ISerializable
    {
        public bool Bar { get; set; }

        public Foo() { }

        //[OnDeserializing()]
        //internal void OnDeserializingMethod(StreamingContext context)
        //{
        //    Bar = 10;
        //}

        //public Foo(SerializationInfo info, StreamingContext context)
        //{
        //    Bar = (int)info.GetValue("Bar", typeof(int));
        //}

        //public void GetObjectData(SerializationInfo info, StreamingContext context)
        //{
        //    info.AddValue("Bar", Bar);
        //}
    }
}

namespace ConsoleApplication2
{
    static class Program
    {
        static void Main(string[] args)
        {
            Foo …
Run Code Online (Sandbox Code Playgroud)

c# serialization blob binaryformatter

3
推荐指数
1
解决办法
1753
查看次数

在C#中使用嵌套List反序列化JSON

如何在C#中使用嵌套List反序列化JSON,我的活动始终为null.请帮我.无法到达我错的地方.任何帮助将非常感激.

我试图反序列化代码,但没有得到活动.我的项目是:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

public class Program
{
    public static void Main()
    {
        string json = @"
            {
  ""Title"": ""test"",
  ""Id"": ""SR21576"",
  ""CreatedDate"": ""2016-12-10T09:21:06+03:30"",
  ""DisplayName"": ""SR21576 : test"",
  ""Description"": ""10"",
  ""AlternateContactMethod"": null,
  ""AffectedUser"": null,
  ""AssignedToUser"": ""XXX\\BB Team"",
  ""CustomerCode"": ""00000000-0000-0000-0000-000000000000"",
  ""CustomerName"": null,
  ""ContractId"": ""00000000-0000-0000-0000-000000000000"",
  ""ContractTitle"": null,
  ""ContractDetailsId"": ""00000000-0000-0000-0000-000000000000"",
  ""ContractDetailsTitle"": null,
  ""CRMLink"": null,
  ""LeadId"": ""00000000-0000-0000-0000-000000000000"",
  ""LeadTitle"": null,
  ""Urgency"": ""b02d9277-a9fe-86f1-e95e-0ba8cd4fd075"",
  ""Priority"": ""1e070214-693f-4a19-82bb-b88ee6362d98"",
  ""Source"": ""57e11419-ac27-a6fa-97f1-0ec2a9722807"",
  ""Area"": ""3c5e6037-b057-5745-4d89-e95f67d3236b"",
  ""SupportGroup"": ""ae536997-8124-85e7-496e-225b7991a450"",
  ""Attachments"": [ ],
  ""Activities"": [
    {
      ""<Id>k__BackingField"": ""ffe60a69-f3c8-adbe-9bbd-4050d766cc81"",
      ""<ActivityID>k__BackingField"": …
Run Code Online (Sandbox Code Playgroud)

c# json json-deserialization

2
推荐指数
1
解决办法
1251
查看次数

自动实现的属性 - 隐藏成员的签名

创建自动实现的属性时,C#会在后台的某个位置为该属性生成成员.我对于我的生活不记得这是什么叫,或者成员的命名约定是什么.谷歌搜索了一段时间后,我认为这可能只是一个想法.

属性:

public int Age { get; set; }
Run Code Online (Sandbox Code Playgroud)

我对隐藏成员的猜测(来自内存):

private int i_age;
Run Code Online (Sandbox Code Playgroud)

编辑#1

为了澄清,我正在寻找自动生成成员的术语,下面是Dmitry Bychenko的回答.术语是"支持领域"

c#

2
推荐指数
1
解决办法
40
查看次数

字段从未分配,但似乎正在使用自动实现的属性

我在这段代码中收到“Field 'UIHandler.handlerNewPlayer' is neverused...”通知:

class UIHandlerMain : IUIHandlerMain
    {
        public UIHandlerMain()
        {
            IUIHandlerNewPlayer handlerNewPlayer = new UIHandlerNewPlayer();
        }

        public IUIHandlerNewPlayer HandlerNewPlayer { get; }
    }
Run Code Online (Sandbox Code Playgroud)

我是 C# 新手,但在我看来,自动实现的 handlerNewPlayer 是在构造函数中设置的。显然,我错过了一些简单而基本的东西。

c#

2
推荐指数
1
解决办法
187
查看次数

Getfield.SetValue不起作用

在我正在研究的项目中,我偶然发现了这个问题:

我想创建一个类"ApiID"的实例.我从Reflector获得了代码,因为你可以看到.dll(不是我的Project)导入来自ummanaged代码,我无权访问.

    [StructLayout(LayoutKind.Sequential, Size=0x10), CLSCompliant(false), NativeCppClass, DebugInfoInPDB, MiscellaneousBits(0x40)]
public struct ApiId
{
    private long <alignment member>;
    public static unsafe void <MarshalCopy>(ApiId* idPtr1, ApiId* idPtr2)
    {
        ApiId.{ctor}(idPtr1, (ApiId modopt(IsConst)* modopt(IsImplicitlyDereferenced)) idPtr2);
    }

    public static unsafe void <MarshalDestroy>(ApiId* idPtr1)
    {
        ApiId.{dtor}(idPtr1);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的C#-Code看起来像这样:

var _apiId = new ApiId();
long vlue = 0x0000000041403070;
typeof(ApiId).GetField("<alignment member>", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(_apiId, vlue);
Run Code Online (Sandbox Code Playgroud)

该代码成功运行,但是Field不会改变并保持0 ...我做错了什么?问候

c# reflection pinvoke

1
推荐指数
1
解决办法
1009
查看次数

在什么情况下,没有为C#中的公共属性生成私有变量?

我在C#中指定一个公共属性,如下所示:

public bool SuezCanalTransversal {get; set;}
Run Code Online (Sandbox Code Playgroud)

但是,似乎没有创建私有成员变量.例如,如果我覆盖get,return _suezCanalTransversal;我看到:

"_suezCanalTransversal does not exist in the current context".

简单地添加相关的私有成员变量是没有问题的,但我的印象是现在已经自动完成了.

几个问题:

何时为公共属性自动生成私有成员变量,何时不是?

当使用诸如public bool SuezCanalTransversal{ get; set;}使用的公共属性时,假设私有变量是否正确:private bool _suezCanalTransversal将被创建?

c# properties

1
推荐指数
1
解决办法
46
查看次数

VB.NET中的类字段可以在没有Getter&Setter吗?

在VB.NET中,我注意到我可以直接使用Property关键字,然后再使用属性名称和数据类型来创建属性,而无需使用getter和setter方法,尽管我无法在C#中做到这一点!

但是,似乎将此属性封装起来,就像将其放入getter和setter方法一样!

请看下面的截图。

在此处输入图片说明

在上面的屏幕截图中,我要谈论的属性是number1,并且我创建了另一个封装在名为number2的getter和setter方法中的属性。

然后,我在Class2中创建了Class1的新实例,但是我注意到在创建类的实例之前,number1属性是不公开的,就像它被封装在类似于number2属性的getter和setter方法!

有什么解释吗?

c# vb.net oop properties getter-setter

-1
推荐指数
1
解决办法
205
查看次数