Class ExpandoBase
Class that provides extensible properties and methods. This dynamic object stores 'extra' properties in a dictionary or checks the actual properties of the instance. This means you can subclass this expando and retrieve either native properties or properties from values in the dictionary.
This type allows you three ways to access its properties:
- Directlyany explicitly declared properties are accessible
- Dynamicdynamic cast allows access to dictionary and native properties/methods
- DictionaryAny of the extended properties are accessible via IDictionary interface
Inherited Members
Namespace: Kephas.Dynamic
Assembly: Kephas.Core.dll
Syntax
public abstract class ExpandoBase : DynamicObject, IExpando, IDynamicMetaObjectProvider, IIndexable
Constructors
| Improve this Doc View SourceExpandoBase(IDictionary<String, Object>)
Initializes a new instance of the ExpandoBase class. This constructor just works off the internal dictionary.
Declaration
protected ExpandoBase(IDictionary<string, object> innerDictionary = null)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | innerDictionary | The inner dictionary for holding dynamic values (optional). If not provided, a new dictionary will be created. |
ExpandoBase(Object, IDictionary<String, Object>)
Initializes a new instance of the ExpandoBase class. Allows passing in an existing instance variable to 'extend'.
Declaration
protected ExpandoBase(object innerObject, IDictionary<string, object> innerDictionary = null)
Parameters
Type | Name | Description |
---|---|---|
System.Object | innerObject | The instance to be extended. |
System.Collections.Generic.IDictionary<System.String, System.Object> | innerDictionary | The inner dictionary for holding dynamic values (optional). If not provided, a new dictionary will be created. |
Properties
| Improve this Doc View SourceItem[String]
Convenience method that provides a string Indexer to the Properties collection AND the strongly typed properties of the object by name. // dynamic exp["Address"] = "112 nowhere lane"; // strong var name = exp["StronglyTypedProperty"] as string;.
Declaration
public object this[string key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The key. |
Property Value
Type | Description |
---|---|
System.Object | The System.Object. |
Remarks
The getter checks the Properties dictionary first then looks in PropertyInfo for properties. The setter checks the instance properties before checking the Properties dictionary.
Methods
| Improve this Doc View SourceGetDynamicMemberNames()
Returns the enumeration of all dynamic member names.
Declaration
public override IEnumerable<string> GetDynamicMemberNames()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.String> | A sequence that contains dynamic member names. |
Overrides
GetInnerObjectTypeInfo()
Gets the ITypeInfo of the inner object.
Declaration
protected virtual ITypeInfo GetInnerObjectTypeInfo()
Returns
Type | Description |
---|---|
ITypeInfo | The ITypeInfo of the inner object. |
GetThisTypeInfo()
Gets the ITypeInfo of this expando object.
Declaration
protected virtual ITypeInfo GetThisTypeInfo()
Returns
Type | Description |
---|---|
ITypeInfo | The ITypeInfo of this expando object. |
HasDynamicMember(String)
Indicates whether the memberName
is defined in the expando.
Declaration
public virtual bool HasDynamicMember(string memberName)
Parameters
Type | Name | Description |
---|---|---|
System.String | memberName | Name of the member. |
Returns
Type | Description |
---|---|
System.Boolean | True if defined, false if not. |
ToDictionary(Func<String, String>, Func<Object, Object>)
Converts the expando to a dictionary having as keys the property names and as values the respective properties' values.
Declaration
public virtual IDictionary<string, object> ToDictionary(Func<string, string> keyFunc = null, Func<object, object> valueFunc = null)
Parameters
Type | Name | Description |
---|---|---|
System.Func<System.String, System.String> | keyFunc | The key transformation function (optional). |
System.Func<System.Object, System.Object> | valueFunc | The value transformation function (optional). |
Returns
Type | Description |
---|---|
System.Collections.Generic.IDictionary<System.String, System.Object> | A dictionary of property values with their associated names. |
TryGetMember(GetMemberBinder, out Object)
Try to retrieve a member by name first from instance properties followed by the collection entries.
Declaration
public override bool TryGetMember(GetMemberBinder binder, out object result)
Parameters
Type | Name | Description |
---|---|---|
System.Dynamic.GetMemberBinder | binder | Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive. |
System.Object | result | The result of the get operation. For example, if the method is called for a property, you can assign the property value to |
Returns
Type | Description |
---|---|
System.Boolean | The System.Boolean. |
Overrides
TryGetValue(String, out Object)
Attempts to get the dynamic value with the given key.
Declaration
protected virtual bool TryGetValue(string key, out object value)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The key. |
System.Object | value | The value to get. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
First of all, it is tried to get a property value from the inner object, if one is set. The next try is to retrieve the property value from the expando object itself. Lastly, if still a property by the provided name cannot be found, the inner dictionary is searched by the provided key.
TryInvokeMember(InvokeMemberBinder, Object[], out Object)
Dynamic invocation method. Currently allows only for Reflection based operation (no ability to add methods dynamically).
Declaration
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
Parameters
Type | Name | Description |
---|---|---|
System.Dynamic.InvokeMemberBinder | binder | Provides information about the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is an instance of the class derived from the System.Dynamic.DynamicObject class, binder.Name returns "SampleMethod". The binder.IgnoreCase property specifies whether the member name is case-sensitive. |
System.Object[] | args | The arguments that are passed to the object member during the invoke operation. For example, for the statement sampleObject.SampleMethod(100), where sampleObject is derived from the System.Dynamic.DynamicObject class, |
System.Object | result | The result of the member invocation. |
Returns
Type | Description |
---|---|
System.Boolean | The System.Boolean. |
Overrides
TrySetMember(SetMemberBinder, Object)
Property setter implementation tries to retrieve value from instance first then into this object.
Declaration
public override bool TrySetMember(SetMemberBinder binder, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Dynamic.SetMemberBinder | binder | Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member to which the value is being assigned. For example, for the statement sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive. |
System.Object | value | The value to set to the member. For example, for sampleObject.SampleProperty = "Test", where sampleObject is an instance of the class derived from the System.Dynamic.DynamicObject class, the |
Returns
Type | Description |
---|---|
System.Boolean | The System.Boolean. |
Overrides
TrySetValue(String, Object)
Attempts to set the value with the given key.
Declaration
protected virtual bool TrySetValue(string key, object value)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | The key. |
System.Object | value | The value to set. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Remarks
First of all, it is tried to set the property value to the inner object, if one is set. The next try is to set the property value to the expando object itself. Lastly, if still a property by the provided name cannot be found, the inner dictionary is used to set the value with the provided key.