小编Aaa*_*aaa的帖子

C#JSON将Dictionary序列化为{key:value,...}而不是{key:key,value:value,...}

是否可以使用以下格式的DataContractJsonSerializer将.Net Dictionary <Key,Value>序列化为JSON :

{
  key0:value0,
  key1:value1,
  ...
}
Run Code Online (Sandbox Code Playgroud)

我使用Dictionary <K,V>,因为没有预定义的输入结构.

我对DataContractJsonSerializer结果感兴趣!我已经找到了一个"Surrogate"示例,但输出中还有一个"数据",如果字典<K,String>,则转义也是假的.


我找到了解决方案,需要什么!首先,一个可序列化的"字典"类:(当然,这个示例只是以一种方式工作,但我不需要反序列化)

[Serializable]
public class MyJsonDictionary<K, V> : ISerializable {
    Dictionary<K, V> dict = new Dictionary<K, V>();

    public MyJsonDictionary() { }

    protected MyJsonDictionary( SerializationInfo info, StreamingContext context ) {
        throw new NotImplementedException();
    }

    public void GetObjectData( SerializationInfo info, StreamingContext context ) {
        foreach( K key in dict.Keys ) {
            info.AddValue( key.ToString(), dict[ key ] );
        }
    }

    public void Add( K key, V value ) …
Run Code Online (Sandbox Code Playgroud)

c# serialization json dictionary

61
推荐指数
5
解决办法
12万
查看次数

DataContract序列化异常(不期望数据协定名称)

我有以下代码:

[DataContract]
class TestContract {
    private String _Name;
    private Int32 _Age;

    [DataMember( Name = "Name" )]
    public String Name {
        get { return _Name; }
        set { _Name = value; }
    }

    [DataMember( Name = "Age" )]
    public Int32 Age {
        get { return _Age; }
        set { _Age = value; }
    }
}

[Serializable]
public class DNCJsonDictionary<K, V> : ISerializable {
    Dictionary<K, V> dict = new Dictionary<K, V>();

    public DNCJsonDictionary() { }

    protected DNCJsonDictionary( SerializationInfo info, StreamingContext context ) …
Run Code Online (Sandbox Code Playgroud)

c# reflection serialization datacontract

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

IOptions 绑定不适用于嵌套类

我有一个工作者服务 ind dotnet core 3.1:

程序.cs:

public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    var builder = new ConfigurationBuilder()
                        .AddJsonFile("appsettings.json", optional: false)
                        .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true)
                        .AddEnvironmentVariables();

                    var configuration = builder.Build();

                    services.AddOptions();

                    // Version 1
                    services.Configure<A>(configuration);

                    // Version 2
                    //services.Configure<A>(o => configuration.GetSection("PollingSettings").Bind(o));

                    services.AddHostedService<Worker>();
                })
                .UseWindowsService();
    }
Run Code Online (Sandbox Code Playgroud)

appsettings.Development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "A": {
    "option1": "value1_from_json",
    "option2": -1, …
Run Code Online (Sandbox Code Playgroud)

c# configuration .net-core

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

封装方法没有参数和返回值

我正在寻找类似Action <T>("封装一个具有单个参数但不返回值的方法")和Func <T,TResult>("封装一个有一个参数并返回值的方法"之类的东西由TResult参数")指定的类型,但我必须封装一个没有参数和返回值的方法.有什么东西,或者我必须写一个?

.net encapsulation function-pointers function

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

如何在C++中使这个初始化合法化?

我在书中看到了一个例外,但我无法弄清楚答案:

以下代码是否合法?如果没有,你怎么能让它合法?

int null = 0, *p = null;

当然,第二个不合法,你不能将int转换为int*.

主题是在该部分constexpr.

伙计们!这只是一个关于指针,consts和constexprs的练习!我想,你必须在没有cast和nullptr的情况下解决它.

c++ c++11

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

XNA 4.0配备英特尔GMA 3150显卡

我有一台配有英特尔GMA 3150显卡的华硕Eee PC,操作系统是Windows 7 Starter,安装了DirectX 11.

当我运行使用XNA 4.0的项目时,我得到以下异常graphics.ApplyChanges():

Microsoft.Xna.Framework.Graphics.NoSuitableGraphicsDeviceException was unhandled
  Message=Could not find a Direct3D device that supports the XNA Framework HiDef profile.

Verify that a suitable graphics device is installed.

Make sure the desktop is not locked, and that no other application is running in full screen mode.

Avoid running under Remote Desktop or as a Windows service.

Check the display properties to make sure hardware acceleration is set to Full.
  Source=Microsoft.Xna.Framework.Game
  StackTrace:
       at Microsoft.Xna.Framework.GraphicsDeviceManager.FindBestPlatformDevice(Boolean anySuitableDevice) …
Run Code Online (Sandbox Code Playgroud)

xna xna-4.0

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

Silverlight - 旋转的TextBlock宽度问题

我有以下网格:

    <Grid Background="LightGray" x:Name="ProgrammPoolTextGrid">
        <TextBlock x:Name="tbLeft" 
                   Margin="0" 
                   Text="PROGRAMMPOOL" 
                   TextWrapping="NoWrap" 
                   Width="100" 
                   Height="94" 
                   RenderTransformOrigin="0.5,0.5" 
                   HorizontalAlignment="Left" 
                   UseLayoutRounding="False" 
                   d:LayoutRounding="Auto" >
            <TextBlock.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="-90"/>
                    <TranslateTransform/>
                </TransformGroup>
            </TextBlock.RenderTransform>
        </TextBlock>
    </Grid>
Run Code Online (Sandbox Code Playgroud)

我只需要一个40像素宽的TextBlock,但如果我改变TextBlock或Grid的宽度,文本将会剪切.任何的想法?

silverlight transform

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

DateTime ToString 坏了?

为了

DateTime aDate = new DateTime(2000,1,1);  
Console.WriteLine(aDate.ToString("d"));
Run Code Online (Sandbox Code Playgroud)

我期待1,但它写道1/1/2000

MSDN说:

d 一个月中的第几天,从 1 到 31。

2009-06-01T13:45:30 -> 1

2009-06-15T13:45:30 -> 15

有什么解决方法吗?对于M.

网络小提琴

c# datetime tostring .net-core

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

MVC问题:直接模式< - >查看通信 - 为什么?

任何人都可以告诉我,为什么直接将模型与MVC模式中的视图进行通信,为什么不直接通过控制器呢?

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

model-view-controller

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