當前位置: 首頁>>代碼示例>>C#>>正文


C# ExpandoObject類代碼示例

本文整理匯總了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());
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:3,代碼來源:ExpandoObject

輸出:

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);
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:10,代碼來源:ExpandoObject

輸出:

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);
    }
}
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:27,代碼來源:ExpandoObject

輸出:

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);
}
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:8,代碼來源:ExpandoObject

輸出:

Name: John Smith
Age: 33

示例5: ExpandoObject

dynamic employee = new ExpandoObject();
employee.Name = "John Smith";
((IDictionary<String, Object>)employee).Remove("Name");
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:3,代碼來源:ExpandoObject

示例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);
    }
}
開發者ID:.NET開發者,項目名稱:System.Dynamic,代碼行數:18,代碼來源:ExpandoObject


注:本文中的System.Dynamic.ExpandoObject類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。