CSharp - 在 C# 中,转储对象以粘土

  显示原文与译文双语对照的内容
0 0

在 C# 中使用ClaySharp将对象转储到控制台/logfile生成的任何方法?

Clay是一种动态 C# 类型,可以像JavaScript或者其他动态语言一样轻松地雕刻任何形状的对象。 项目链接

使用粘土构建对象:


//Build a Clay object
dynamic New = new ClayFactory();

dynamic directory = New.Array(
 New.Person(
 FirstName:"Louis",
 LastName:"Dejardin",
 Aliases: new[] {"Lou" }
 ),
 New.Person(
 FirstName:"Bertrand",
 LastName:"Le Roy"
 ).Aliases("bleroy","boudin"),
 New.Person(
 FirstName:"Renaud",
 LastName:"Paquay"
 ).Aliases("Your Scruminess","Chef")
).Name("Some Orchard folks");

时间: 原作者:

0 0

我认为这可能需要添加一个特性来粘土,后细节我们生活在一个 private 字段名为 _behaviour ( 集合) 内 PropBehaviour private 野外 _props 收集的属性,并在 ArrayBehaviour private _data 字段的数组。

也许一个新 ClayBehavior ( 或者更可能是 PropBehaviour的扩展) 类似于 ArrayBehaviourGetEnumerator,列出所有属性。


更新: 尝试:


 public string DumpClay(dynamic clay, int indent = 0, bool appendLine = true)
 {
 var indentStr = new string('t', indent);

 if (!(clay is Clay))
 {
 return indentStr + clay.ToString()
 + (appendLine? ((indent == 0)?";" :",") + Environment.NewLine :"");
 }


 var sb = new StringBuilder();

 if (IsArray(clay))
 {
 sb.Append(indentStr).AppendLine("[");
 foreach (var item in clay)
 {
 sb.AppendLine(DumpClay(item, indent + 2));
 }
 sb.Append(indentStr).Append("]");
 }

 PropBehavior behaviour;
 if (IsProp(clay, out behaviour))
 {
 if (sb.Length> 0)
 {
 sb.AppendLine();
 }

 sb.Append(indentStr).AppendLine("{");
 foreach (var pair in GetProps(behaviour))
 {
 sb.Append(DumpClay(pair.Key, indent + 1, appendLine: false)).AppendLine(":")
. Append(DumpClay(pair.Value, indent + 2));
 }
 sb.Append(indentStr).Append("}");
 }

 if (appendLine)
 {
 sb.AppendLine((indent == 0)?";" :",");
 }

 return sb.ToString();
 }

 private static bool IsArray(Clay clay)
 {
 var behaviours = GetBehaviorCollection(clay);

 var arrayBehaviour = behaviours.FirstOrDefault(clayBehavior => clayBehavior is ArrayBehavior);

 return (arrayBehaviour!= null);
 }

 private static bool IsProp(Clay clay, out PropBehavior propBehavior)
 {
 var behaviours = GetBehaviorCollection(clay);

 propBehavior = (PropBehavior)behaviours.FirstOrDefault(clayBehavior => clayBehavior is PropBehavior);

 return (propBehavior!= null);
 }

 private static IEnumerable<IClayBehavior> GetBehaviorCollection(Clay clay)
 {
//ReSharper disable PossibleNullReferenceException
 return (ClayBehaviorCollection)typeof(Clay)
. GetField("_behavior", BindingFlags.NonPublic | BindingFlags.Instance)
. GetValue(clay);
//ReSharper restore PossibleNullReferenceException
 }

 private static IEnumerable<KeyValuePair<object, object>> GetProps(PropBehavior propBehavior)
 {
//ReSharper disable PossibleNullReferenceException
 return (Dictionary<object, object>)typeof(PropBehavior)
. GetField("_props", BindingFlags.NonPublic | BindingFlags.Instance)
. GetValue(propBehavior);
//ReSharper restore PossibleNullReferenceException
 }


上面的示例输出:

[
 {
 ShapeName:
 Person,
 FirstName:
 Louis,
 LastName:
 Dejardin,
 Aliases:
 System.String[],
 },
 {
 ShapeName:
 Person,
 FirstName:
 Bertrand,
 LastName:
 Le Roy,
 Aliases:
 [
 bleroy,
 boudin,
 ]
 {
 },
 },
 {
 ShapeName:
 Person,
 FirstName:
 Renaud,
 LastName:
 Paquay,
 Aliases:
 [
 Your Scruminess,
 Chef,
 ]
 {
 },
 },
]
{
 Name:
 Some Orchard folks,
};

原作者:
...