[英].NET Serialization getters and setters
I want to try a .NET deserialization example, but it seems I am not able to get the getters and setters working. 我想尝试一个.NET反序列化示例,但似乎无法使getter和setter正常工作。 This is my code 这是我的代码
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace WindowsFormsApplication3
{
[XmlRoot]
public class TestClass
{
public string classname;
private string name;
private int age;
[XmlAttribute]
public string Classname { get => classname; set => classname = value; }
[XmlElement]
public string Name { get => name; set => name = value; }
[XmlElement]
public int Age { get=>age; set => age = value; }
public override string ToString()
{
return base.ToString();
}
}
class Program
{
static void Main(string[] args)
{
TestClass testClass = new TestClass();
testClass.Classname = "test";
testClass.Name = "william";
testClass.Age = 50;
Console.WriteLine("Hello World!");
MessageBox.Show("Test");
}
}
}
And I get the following error in the get declaration: Not all code paths return a value
而且在get声明中出现以下错误: Not all code paths return a value
As commented by @CodeCaster, you need a minimum of C# 7.0 to work around on Expression-Bodied Members and your visual studio doesn't support it. 正如@CodeCaster所评论的那样,您至少需要C#7.0才能在Expression- Bodied Member上变通,而Visual Studio不支持它。
So you can upgrade your visual studio to C# 7.0 or use below with current version, 因此,您可以将Visual Studio升级到C#7.0或在当前版本中使用以下版本,
You can use 您可以使用
public string Classname
{
get { return classname; }
set { classname = value; }
}
instead of 代替
public string Classname
{
get => classname;
set => classname = value;
}
And do the same for all other remaining properties in your class those are with expression-bodies. 并对类中具有表达式主体的所有其他其余属性执行相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.