我得到了一个StackOverflowException(是!:D)尝试在构造函数中设置类属性,如下所示:
namespace WindowsUpdateOnLan
{
public class NetworkAdapter
{
public NetworkAdapter(PropertyDataCollection properties)
{
String value = null;
foreach (PropertyData pd in properties)
{
if (pd.Name.Equals("GUID"))
Id = Guid.Parse(pd.Value.ToString());
if (pd.Name.Equals("Name"))
Name = pd.Value.ToString();
if (pd.Name.Equals("NetConnectionID"))
{
value = Regex.Replace(pd.Value.ToString(), @"\s+", "");
adapterType = (AdapterTypeEnum)Enum.Parse(typeof(AdapterTypeEnum), value);
}
if (pd.Name.Equals("NetEnabled"))
{
value = Regex.Replace(pd.Value.ToString(), @"\s+", "");
adapterStatus = (AdapterStatusEnum)Enum.Parse(typeof(AdapterStatusEnum), value);
}
}
}
/// <summary>
/// Contains the GUID that is used to identify the adapter
/// </summary>
public Guid Id
{
get { return this.Id; }
private set { Id = value; }
}
Visual Studio告诉我确保我没有无限循环。
我一定忘记了重要的事情,或者语法不对。
有人可以看一下吗?
答案 0 :(得分:9)
public Guid Id
{
get { return this.Id; }
private set { Id = value; }
}
你在getter / setter中调用相同的属性getter / setter。因此递归。
解决方案:
id
并使用它。public Guid Id { get; private set; }