08-10-12, 11:44 AM
The DynamicObject Class
يوفر هذا النوع قابلية التحكم في العمليات التي يتم إجراؤها على الكائنات الحيوية وكيفية تنفيذ تلك العمليات, على سبيل المثال تستطيع التحكم في كيفية الوصول إلى خاصية من خصائص كائن حيوي, أو استدعاء إجراء , أو القيام بالعمليات الرياضية مثل الجمع والضرب... استخدام هذا النوع مفيد أحياناً عندما تتعامل مع مكتبة Library أو Add-in تستخدم فيها Syntax مثل : (scriptObject.SetProperty("PropertyName", propertyValue يمكنك في هذه الحالة باستخدام DynamicObject استخدام Syntax أكثر بساطة مثل scriptObject.PropertyName = propertValue.
لا يمكنك إنشاء نسخة من نوع DynamicObject مباشرة, بل عليك ببناء فئتك الخاصة وتوريثها من النوع DynamicObject والقيام بإعادة تعريف Overriding للإجراءات الضرورية , على سبيل المثال, إذا أردت إجراء عمليات من أجل إسناد قيم وقرائتها فقط , تحتاج عندها فقط لإعادة تعريف الإجرائين TrySetMember و TryGetMember . ولاستخدام نسخة من الفئة الجديدة في #C لابد من استخدام dynamic Keyword . بينما في VB يستخدم الربط المتأخر بواسطة Object Class.
المثال التالي يوضح كيفية استخدام DynamicObject Class من أجل بناء نوع جديد موروث من ذلك النوع وإنشاء نسخ منه وكيفية استخدامه وهذا المثال مبني على المتطلبات التالية: لنفترض أنك تريد توفير Syntax بديل عند التعامل مع Dictionary على سبيل المثال, فبدل من أن تكتب myDicionary("someKey") = someValue , يمكنك كتابة myDictionay.someKey = someValue بشكل مباشر . كما أنك ايضاً تريد أن يكون هذا التركيب غير حساس لحالة الأحرف case-insensitive . المثال التالي هو عبارة عن فئة تحتوي على كائن من نوع IDictionaty<String, Object> يستخدم للقيام بحفظ القيم على شكل ازواج key/value pairs داخل هذا Dictionary :
أولاً : أضف Class جديد إلى مشروعك, وفي أعلاه قم باستيراد مجال الأسماء System.Dynamic ثم قم بتعريف فئة جديدة باسم DynamicDictionary وقم بتوريثها من الفئة DynamicObject , لاحظ أنه يمكنك أيضاً إضافة أعضاء ثابتة Static إلى هذه الفئة كما هو الحال في المثال التالي في الخاصية Count (والمقصود هنا بالثابتة أنه لايمكنك حذفها)وفي هذه الحالة يجب الانتباه إلى أن DLR عند استدعاء أي عضو يقوم بالبحث أولاً في الأعضاء الثابتة فإذا لم يجد أي تطابق سيقوم باستدعاء الإجراء TryGetMember .المثال موضح في الكود التالي:
في النهاية ما يمكن قوله هو أن هذه الإضافة لها استخدامات عديدة ومتقدمة وهذا ما هو إلا جزء بسيط فقط من أجل توضيح مبدأ ال Dynamic وتوضيح طرق استخدامها.
بالتوفيق للجميع.
يوفر هذا النوع قابلية التحكم في العمليات التي يتم إجراؤها على الكائنات الحيوية وكيفية تنفيذ تلك العمليات, على سبيل المثال تستطيع التحكم في كيفية الوصول إلى خاصية من خصائص كائن حيوي, أو استدعاء إجراء , أو القيام بالعمليات الرياضية مثل الجمع والضرب... استخدام هذا النوع مفيد أحياناً عندما تتعامل مع مكتبة Library أو Add-in تستخدم فيها Syntax مثل : (scriptObject.SetProperty("PropertyName", propertyValue يمكنك في هذه الحالة باستخدام DynamicObject استخدام Syntax أكثر بساطة مثل scriptObject.PropertyName = propertValue.
لا يمكنك إنشاء نسخة من نوع DynamicObject مباشرة, بل عليك ببناء فئتك الخاصة وتوريثها من النوع DynamicObject والقيام بإعادة تعريف Overriding للإجراءات الضرورية , على سبيل المثال, إذا أردت إجراء عمليات من أجل إسناد قيم وقرائتها فقط , تحتاج عندها فقط لإعادة تعريف الإجرائين TrySetMember و TryGetMember . ولاستخدام نسخة من الفئة الجديدة في #C لابد من استخدام dynamic Keyword . بينما في VB يستخدم الربط المتأخر بواسطة Object Class.
المثال التالي يوضح كيفية استخدام DynamicObject Class من أجل بناء نوع جديد موروث من ذلك النوع وإنشاء نسخ منه وكيفية استخدامه وهذا المثال مبني على المتطلبات التالية: لنفترض أنك تريد توفير Syntax بديل عند التعامل مع Dictionary على سبيل المثال, فبدل من أن تكتب myDicionary("someKey") = someValue , يمكنك كتابة myDictionay.someKey = someValue بشكل مباشر . كما أنك ايضاً تريد أن يكون هذا التركيب غير حساس لحالة الأحرف case-insensitive . المثال التالي هو عبارة عن فئة تحتوي على كائن من نوع IDictionaty<String, Object> يستخدم للقيام بحفظ القيم على شكل ازواج key/value pairs داخل هذا Dictionary :
أولاً : أضف Class جديد إلى مشروعك, وفي أعلاه قم باستيراد مجال الأسماء System.Dynamic ثم قم بتعريف فئة جديدة باسم DynamicDictionary وقم بتوريثها من الفئة DynamicObject , لاحظ أنه يمكنك أيضاً إضافة أعضاء ثابتة Static إلى هذه الفئة كما هو الحال في المثال التالي في الخاصية Count (والمقصود هنا بالثابتة أنه لايمكنك حذفها)وفي هذه الحالة يجب الانتباه إلى أن DLR عند استدعاء أي عضو يقوم بالبحث أولاً في الأعضاء الثابتة فإذا لم يجد أي تطابق سيقوم باستدعاء الإجراء TryGetMember .المثال موضح في الكود التالي:
VB Code
كود :
' The class derived from DynamicObject.
Public Class DynamicDictionary
Inherits DynamicObject
' The inner dictionary.
Dim dictionary As New Dictionary(Of String, Object)
' This property returns the number of elements
' in the inner dictionary.
ReadOnly Property Count As Integer
Get
Return dictionary.Count
End Get
End Property
' If you try to get a value of a property that is
' not defined in the class, this method is called.
Public Overrides Function TryGetMember(
ByVal binder As System.Dynamic.GetMemberBinder,
ByRef result As Object) As Boolean
' Converting the property name to lowercase
' so that property names become case-insensitive.
Dim name As String = binder.Name.ToLower()
' If the property name is found in a dictionary,
' set the result parameter to the property value and return true.
' Otherwise, return false.
Return dictionary.TryGetValue(name, result)
End Function
Public Overrides Function TrySetMember(
ByVal binder As System.Dynamic.SetMemberBinder,
ByVal value As Object) As Boolean
' Converting the property name to lowercase
' so that property names become case-insensitive.
dictionary(binder.Name.ToLower()) = value
' You can always add a value to a dictionary,
' so this method always returns true.
Return True
End Function
End Class
Sub Main()
' Creating a dynamic dictionary.
Dim person As Object = New DynamicDictionary()
' Adding new dynamic properties.
' The TrySetMember method is called.
person.FirstName = "Ellen"
person.LastName = "Adams"
' Getting values of the dynamic properties.
' The TryGetMember method is called.
' Note that property names are now case-insensitive,
' although they are case-sensitive in C#.
Console.WriteLine(person.firstname & " " & person.lastname)
' Getting the value of the Count property.
' The TryGetMember is not called,
' because the property is defined in the class.
Console.WriteLine("Number of dynamic properties:" & person.Count)
' The following statement throws an exception at run time.
' There is no "address" property,
' so the TryGetMember method returns false and this causes
' a MissingMemberException.
' Console.WriteLine(person.address)
End Sub
' This examples has the following output:
' Ellen Adams
' Number of dynamic properties: 2C# Code
كود :
// The class derived from DynamicObject.
public class DynamicDictionary : DynamicObject
{
// The inner dictionary.
Dictionary<string, object> dictionary
= new Dictionary<string, object>();
// This property returns the number of elements
// in the inner dictionary.
public int Count
{
get
{
return dictionary.Count;
}
}
// If you try to get a value of a property
// not defined in the class, this method is called.
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary,
// set the result parameter to the property value and return true.
// Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is
// not defined in the class, this method is called.
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
// Converting the property name to lowercase
// so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
}
class Program
{
static void Main(string[] args)
{
// Creating a dynamic dictionary.
dynamic person = new DynamicDictionary();
// Adding new dynamic properties.
// The TrySetMember method is called.
person.FirstName = "Ellen";
person.LastName = "Adams";
// Getting values of the dynamic properties.
// The TryGetMember method is called.
// Note that property names are case-insensitive.
Console.WriteLine(person.firstname + " " + person.lastname);
// Getting the value of the Count property.
// The TryGetMember is not called,
// because the property is defined in the class.
Console.WriteLine(
"Number of dynamic properties:" + person.Count);
// The following statement throws an exception at run time.
// There is no "address" property,
// so the TryGetMember method returns false and this causes a
// RuntimeBinderException.
// Console.WriteLine(person.address);
}
}
// This example has the following output:
// Ellen Adams
// Number of dynamic properties: 2في النهاية ما يمكن قوله هو أن هذه الإضافة لها استخدامات عديدة ومتقدمة وهذا ما هو إلا جزء بسيط فقط من أجل توضيح مبدأ ال Dynamic وتوضيح طرق استخدامها.
بالتوفيق للجميع.
