小编Def*_*ult的帖子

XmlSerializer列表项元素名称

我上课了 PersonList

[XmlRoot("Persons")]
PersonList : List<Human>
Run Code Online (Sandbox Code Playgroud)

当我将其序列化为XML时,默认情况下会生成如下内容:

<Persons>
  <Human>...</Human>
  <Human>...</Human>
</Persons>
Run Code Online (Sandbox Code Playgroud)

我的问题是需要以改变元素做什么Human,以Person输出?所以输出将是:

<Persons>
  <Person>...</Person>
  <Person>...</Person>
</Persons>
Run Code Online (Sandbox Code Playgroud)

并且,如何将上述XML反序列化为PersonList类对象?

Per Nick的建议,这是我的测试代码:

[XmlRoot("Persons")]
public class Persons : List<Human>
{

}

[XmlRoot("Person")]
public class Human
{
    public Human()
    {
    }

    public Human(string name)
    {
        Name = name;
    }

    [XmlElement("Name")]
    public string Name { get; set; }

}

void TestXmlSerialize()
{
    Persons personList = new Persons();
    personList.Add(new Human("John"));
    personList.Add(new Human("Peter"));

    try
    {
        using (StringWriter writer = new StringWriter())
        { …
Run Code Online (Sandbox Code Playgroud)

c# xmlserializer

37
推荐指数
3
解决办法
6万
查看次数

使用LINQ进行字母数字排序

我有一个string[],其中每个元素都以一些数字值结尾.

string[] partNumbers = new string[] 
{ 
    "ABC10", "ABC1","ABC2", "ABC11","ABC10", "AB1", "AB2", "Ab11" 
};
Run Code Online (Sandbox Code Playgroud)

我试图按如下方式对上面的数组进行排序,LINQ但我没有得到预期的结果.

var result = partNumbers.OrderBy(x => x);
Run Code Online (Sandbox Code Playgroud)

实际结果:

AB1
Ab11
AB2
ABC1
ABC10
ABC10
ABC11
ABC2

预期结果

AB1
AB2
AB11
..

c# linq sorting

35
推荐指数
4
解决办法
3万
查看次数

直接修改List <T>元素

我有这个结构:

struct Map
{
    public int Size;

    public Map ( int size )
    {
        this.Size = size;
    }

    public override string ToString ( )
    {
        return String.Format ( "Size: {0}", this.Size );
    }
}
Run Code Online (Sandbox Code Playgroud)

使用数组时,它可以工作:

Map [ ] arr = new Map [ 4 ] {
    new Map(10),
    new Map(20),
    new Map(30),
    new Map(40)};

arr [ 2 ].Size = 0;
Run Code Online (Sandbox Code Playgroud)

但是当使用List时,它不会编译:

List<Map> list = new List<Map> ( ) {
    new Map(10),
    new Map(20),
    new Map(30),
    new Map(40)};

list [ 2 …
Run Code Online (Sandbox Code Playgroud)

.net c#

33
推荐指数
2
解决办法
5万
查看次数

从电子邮件地址获取域名

我有一个电子邮件地址

xyz@yahoo.com
Run Code Online (Sandbox Code Playgroud)

我想从电子邮件地址获取域名.我能用Regex实现这个目标吗?

c# email domain-name

31
推荐指数
3
解决办法
3万
查看次数

无法找到"ContentProvider"的提供商信息

我有一个问题,我只是想不通.我使用Eclipse创建自己的内容提供程序,但不断收到以下错误:

[..] ERROR/ActivityThread(1051):无法找到my.package.provider.countrycontentprovider的提供商信息

代码见:http://codepad.org/Rx00HjHd

主要部分:

public class CountryContentProvider extends ContentProvider {

    public static final String PROVIDER = 
         "my.package.provider.countrycontentprovider";
    public static final Uri CONTENT_URI = 
         Uri.parse("content://" + PROVIDER + "/country");
    // ...
    @Override
    public boolean onCreate() { return true; }
    // ...
}


// from my activity
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(CountryContentProvider.CONTENT_URI, 
                                  null, null, null, null);  

// AndroidManifest.xml
<provider
    android:name="my.package.provider.CountryContentProvider"
    android:authorities="my.package.provider.countrycontentprovider" />
Run Code Online (Sandbox Code Playgroud)

我已将提供程序添加到清单中,并从onCreate函数返回true .我CountryContentProvider.CONTENT_URI在我的活动中使用来自我的提供者的内容,但我只是不断收到该错误消息.我删除并添加了三次代码(在日食融化的情况下)无济于事.
我肯定错过了什么.有人能指出我正确的方向吗?

android android-contentprovider

30
推荐指数
2
解决办法
5万
查看次数

System.Version未序列化

我有一个System.Version属性的类,看起来像这样:

    • 建立:111
    • 专业:1
    • MajorRevision:0
    • 轻微:1
    • MinorRevision:10
    • 修订:10

当我序列化类时,版本始终为空:

<Version />
Run Code Online (Sandbox Code Playgroud)

Client类看起来像:

[Serializable]
public class Client
{
    public string Description;
    public string Directory;
    public DateTime ReleaseDate;
    public Version Version;
}
Run Code Online (Sandbox Code Playgroud)

c# xml serialization version

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

如何在app.config中添加换行符?

我不知道如何在app.config中编写一条包含两行或更多行的消息.我在配置文件中的常用代码是:

add key="msg_test" value="This is test message."       
Run Code Online (Sandbox Code Playgroud)

我用简单的代码阅读它:

ConfigurationSettings.AppSettings[msg_test];
Run Code Online (Sandbox Code Playgroud)

我想写点类似的东西

add key="msg_test" 
    value="This is test message \n are you sure you want to continue?"
Run Code Online (Sandbox Code Playgroud)

c# app-config

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

前瞻性声明的危险是什么?

我刚接受采访.我被问到什么是"前瞻性声明".然后我被问到他们是否是与前瞻性声明有关的危险.

我无法回答第二个问题.在网上搜索没有显示任何有趣的结果.

那么,有人知道与使用前向声明有关的任何危险吗?

c++ forward-declaration

24
推荐指数
3
解决办法
4203
查看次数

将float转换为double丢失精度但不通过ToString

我有以下代码:

float f = 0.3f;
double d1 = System.Convert.ToDouble(f);
double d2 = System.Convert.ToDouble(f.ToString());
Run Code Online (Sandbox Code Playgroud)

结果相当于:

d1 = 0.30000001192092896;
d2 = 0.3;
Run Code Online (Sandbox Code Playgroud)

我很想知道为什么会这样?

c# string floating-point precision double

22
推荐指数
2
解决办法
4万
查看次数

组织包括

  • 是否有一些首选方式来组织包含指令?
  • 是否更好地在文件中包含所需的.cpp文件而不是.h文件?翻译单位是否受到影响?
  • 如果我在.h文件和.cpp文件中都需要它,我应该将它包含在.h文件中吗?这有关系吗?
  • 将已定义的文件保存在预编译的头文件(stdafx.h)中,例如std和第三方库,这是一个好习惯吗?我自己的文件怎么样,我应该在stdafx.h创建它们的过程中将它们包含在文件中?

// myClass.h
#include <string>
// ^-------- should I include it here? --------

class myClass{
    myClass();
    ~myClass();

    int calculation()
};

// myClass.cpp
#include "myClass.h"
#include <string>
//  ^-------- or maybe here?  --------

[..]

int myClass::calculation(){
    std::string someString = "Hello World";
    return someString.length();
}


// stdafx.h
#include <string.h>
// ^--------- or perhaps here, and then include stdafx.h everywhere? -------
Run Code Online (Sandbox Code Playgroud)

c++ include

22
推荐指数
3
解决办法
4474
查看次数