using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace WmiDataAnalyzer
{
public partial class VirDirSetting
{
//所屬子網站
public List<VirDirSetting> Children = new List<VirDirSetting>();
//父網站名稱
public string ParentName;
//父網站物件
public VirDirSetting Parent;
//所有可能的父網站名稱
public string[] AncestorNames
{
get
{
var p = this.Name.Split('/');
return Enumerable.Range(1, p.Length - 1)
.Select(i => string.Join("/", p.Take(i).ToArray())).ToArray();
}
}
//層級深度
public int Level
{
get { return this.Name.Split('/').Length - 2; }
}
//動態比對屬性用屬性集合
static Dictionary<string, PropertyInfo> Properties
{
get
{
return typeof(VirDirSetting)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(o => !"AncestorNames,Level,ScriptMaps".Split(',').Contains(o.Name))
.ToDictionary(o => o.Name, o => o);
}
}
//與Parent比較,找出有差異的設定
public Dictionary<string, string> GetExplicitSettings()
{
var diff = new Dictionary<string, string>();
if (this.Parent == null)
{
diff.Add("Remark", "**Root**");
}
else
{
foreach (var p in VirDirSetting.Properties.Values)
{
var pv = JsonConvert.SerializeObject(p.GetValue(this.Parent));
var cv = JsonConvert.SerializeObject(p.GetValue(this));
if (pv.CompareTo(cv) != 0)
diff.Add(p.Name, cv);
}
}
return diff;
}
}
}