本文整理汇总了C#中MemberPath.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# MemberPath.Clear方法的具体用法?C# MemberPath.Clear怎么用?C# MemberPath.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemberPath
的用法示例。
在下文中一共展示了MemberPath.Clear方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMyClass
public void TestMyClass()
{
var testClass = new MyClass { Sub = new MyClass() };
testClass.Maps["XXX"] = new MyClass();
testClass.Subs.Add(new MyClass());
// 1) MyClass.Value = 1
var memberPath = new MemberPath();
memberPath.Push(MemberValue);
object value;
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
Assert.IsTrue(memberPath.TryGetValue(testClass, out value));
Assert.AreEqual(1, value);
Assert.AreEqual(1, testClass.Value);
// 2) MyClass.Sub.Value = 1
memberPath.Clear();
memberPath.Push(MemberSub);
memberPath.Push(MemberValue);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
Assert.IsTrue(memberPath.TryGetValue(testClass, out value));
Assert.AreEqual(1, value);
Assert.AreEqual(1, testClass.Sub.Value);
// 3) MyClass.Struct.X = 1
memberPath.Clear();
memberPath.Push(MemberStruct);
memberPath.Push(MemberX);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
Assert.IsTrue(memberPath.TryGetValue(testClass, out value));
Assert.AreEqual(1, value);
Assert.AreEqual(1, testClass.Struct.X);
// 3) MyClass.Maps["XXX"].Value = 1
memberPath.Clear();
memberPath.Push(MemberMaps);
memberPath.Push(MapClassDesc, "XXX");
memberPath.Push(MemberValue);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
Assert.IsTrue(memberPath.TryGetValue(testClass, out value));
Assert.AreEqual(1, value);
Assert.AreEqual(1, testClass.Maps["XXX"].Value);
// 4) MyClass.Subs[0].Value = 1
memberPath.Clear();
memberPath.Push(MemberSubs);
memberPath.Push(ListClassDesc, 0);
memberPath.Push(MemberValue);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
Assert.IsTrue(memberPath.TryGetValue(testClass, out value));
Assert.AreEqual(1, value);
Assert.AreEqual(1, testClass.Subs[0].Value);
// 5) MyClass.Subs[0].X (invalid)
memberPath.Clear();
memberPath.Push(MemberSubs);
memberPath.Push(ListClassDesc, 0);
memberPath.Push(MemberX);
Assert.IsFalse(memberPath.TryGetValue(testClass, out value));
Assert.IsFalse(memberPath.Apply(testClass, MemberPathAction.ValueSet, 1));
// 6) Remove key MyClass.Maps.Remove("XXX")
memberPath.Clear();
memberPath.Push(MemberMaps);
memberPath.Push(MapClassDesc, "XXX");
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.DictionaryRemove, null));
Assert.IsFalse(testClass.Maps.ContainsKey("XXX"));
// 7) Re-add a value to the dictionary
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.ValueSet, new MyClass()));
Assert.IsTrue(testClass.Maps.ContainsKey("XXX"));
// 8) Remove key MyClass.Subs.Remove(0)
memberPath.Clear();
memberPath.Push(MemberSubs);
memberPath.Push(ListClassDesc, 0);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.CollectionRemove, null));
Assert.AreEqual(0, testClass.Subs.Count);
// 9) Add a key MyClass.Subs.Add(new MyClass())
memberPath.Clear();
memberPath.Push(MemberSubs);
memberPath.Push(ListClassDesc, 0);
Assert.IsTrue(memberPath.Apply(testClass, MemberPathAction.CollectionAdd, new MyClass()));
Assert.AreEqual(1, testClass.Subs.Count);
}