本文整理汇总了C#中System.Dynamic.ExpandoObject类的典型用法代码示例。如果您正苦于以下问题:C# ExpandoObject类的具体用法?C# ExpandoObject怎么用?C# ExpandoObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ExpandoObject类属于System.Dynamic命名空间,在下文中一共展示了ExpandoObject类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
sampleObject.test = "Dynamic Property";
Console.WriteLine(sampleObject.test);
Console.WriteLine(sampleObject.test.GetType());
输出:
Dynamic Property System.String
示例2:
sampleObject.number = 10;
sampleObject.Increment = (Action)(() => { sampleObject.number++; });
// Before calling the Increment method.
Console.WriteLine(sampleObject.number);
sampleObject.Increment();
// After calling the Increment method.
Console.WriteLine(sampleObject.number);
输出:
10 11
示例3: Main
class Program
{
static void Main(string[] args)
{
dynamic employee, manager;
employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
manager = new ExpandoObject();
manager.Name = "Allison Brown";
manager.Age = 42;
manager.TeamSize = 10;
WritePerson(manager);
WritePerson(employee);
}
private static void WritePerson(dynamic person)
{
Console.WriteLine("{0} is {1} years old.",
person.Name, person.Age);
// The following statement causes an exception
// if you pass the employee object.
// Console.WriteLine("Manages {0} people", person.TeamSize);
}
}
输出:
John Smith is 33 years old. Allison Brown is 42 years old.
示例4: ExpandoObject
dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
employee.Age = 33;
foreach (var property in (IDictionary<String, Object>)employee)
{
Console.WriteLine(property.Key + ": " + property.Value);
}
输出:
Name: John Smith Age: 33
示例5: ExpandoObject
dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
((IDictionary<String, Object>)employee).Remove("Name");
示例6: Test
// Add "using System.ComponentModel;" line
// to the beginning of the file.
class Program
{
static void Test()
{
dynamic employee = new ExpandoObject();
((INotifyPropertyChanged)employee).PropertyChanged +=
new PropertyChangedEventHandler(HandlePropertyChanges);
employee.Name = "John Smith";
}
private static void HandlePropertyChanges(
object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("{0} has changed.", e.PropertyName);
}
}