Class DynamicObjectExtensions
Inheritance
Namespace:InfinniPlatform.Dynamic
Assembly:InfinniPlatform.Core.Abstractions.dll
Syntax
public static class DynamicObjectExtensions : object
Methods
| Improve this Doc View SourceTryGetPropertyValue(Object, String)
Returns the property value of a specified object.
Declaration
public static object TryGetPropertyValue(this object target, string propertyName)
Parameters
Type | Name | Description |
---|---|---|
System.Object | target | The object whose property value will be returned. |
System.String | propertyName | The string containing the name of the property to get. |
Returns
Type | Description |
---|---|
System.Object | The property value of the specified object or |
Remarks
This method tryes to get the property value of a specified object using the dynamic context.
If the first attempt complets with an null
is returned.
Examples
Using with typed instances:
class A
{
[SerializerPropertyName("p1")]
public int Property1 { get; set; }
}
var target = new A { Property1 = 123 };
// Access to the property by name
var value1 = target.TryGetPropertyValue("Property1"); // 123
// Access to the property by alias
var value2 = target.TryGetPropertyValue("p1"); // 123
Using with dynamic instances:
var target = new DynamicDocument { { "Property1", 123 } };
var value = target.TryGetPropertyValue("Property1"); // 123
|
Improve this Doc
View Source
TryGetPropertyValueByPath(Object, String)
Returns the property value of a specified object.
Declaration
public static object TryGetPropertyValueByPath(this object target, string propertyPath)
Parameters
Type | Name | Description |
---|---|---|
System.Object | target | The object whose property value will be returned. |
System.String | propertyPath | The string containing the path of the property to get. |
Returns
Type | Description |
---|---|
System.Object | The property value of the specified object or |
Remarks
The path of the property propertyPath is a reference to the property by using the dot notation.
Property.SubProperty.SubSubProperty
Each term of the path is a property name or collection index (if previous property is a collection).
If the property path is not found null
is returned.
Examples
Using with typed instances:
class A
{
[SerializerPropertyName("p1")]
public B Property1 { get; set; }
}
class B
{
[SerializerPropertyName("p2")]
public int Property2 { get; set; }
}
var target = new A { Property1 = new B { Property2 = 123 } };
// Access to the property by name
var value1 = target.TryGetPropertyValueByPath("Property1.Property2"); // 123
// Access to the property by alias
var value2 = target.TryGetPropertyValueByPath("p1.p2"); // 123
Using with dynamic instances:
var target = new DynamicDocument { { "Property1", new DynamicDocument { { "Property2", 123 } } } };
var value = target.TryGetPropertyValueByPath("Property1.Property2"); // 123
Using with collection properties:
class C
{
[SerializerPropertyName("p1")]
public B[] Property1 { get; set; }
}
class B
{
[SerializerPropertyName("p2")]
public int Property2 { get; set; }
}
var target = new C { Property1 = new[] { new B { Property2 = 123 } } };
// Access to the property by name
var value1 = target.TryGetPropertyValueByPath("Property1.0.Property2"); // 123
// Access to the property by alias
var value2 = target.TryGetPropertyValueByPath("p1.0.p2"); // 123
|
Improve this Doc
View Source
TrySetPropertyValue(Object, String, Object)
Sets the property value of a specified object.
Declaration
public static void TrySetPropertyValue(this object target, string propertyName, object propertyValue)
Parameters
Type | Name | Description |
---|---|---|
System.Object | target | The object whose property value will be set. |
System.String | propertyName | The string containing the name of the property to set. |
System.Object | propertyValue | The new property value. |
Remarks
This method tryes to set the property value of a specified object using the dynamic context.
If the first attempt complets with an
Examples
Using with typed instances:
class A
{
[SerializerPropertyName("p1")]
public int Property1 { get; set; }
}
var target = new A();
// Access to the property by name
target.TrySetPropertyValue("Property1", 123); // target.Property1 == 123
// Access to the property by alias
target.TrySetPropertyValue("p1", 123); // target.Property1 == 123
Using with dynamic instances:
var target = new DynamicDocument();
target.TrySetPropertyValue("Property1", 123); // target.Property1 == 123
|
Improve this Doc
View Source
TrySetPropertyValueByPath(Object, String, Object)
Sets the property value of a specified object.
Declaration
public static void TrySetPropertyValueByPath(this object target, string propertyPath, object propertyValue)
Parameters
Type | Name | Description |
---|---|---|
System.Object | target | The object whose property value will be set. |
System.String | propertyPath | The string containing the path of the property to set. |
System.Object | propertyValue | The new property value. |
Remarks
The path of the property propertyPath is a reference to the property by using the dot notation.
Property.SubProperty.SubSubProperty
Each term of the path is a property name or collection index (if previous property is a collection).
If the property path is not found nothing happens.
Examples
Using with typed instances:
class A
{
[SerializerPropertyName("p1")]
public B Property1 { get; set; }
}
class B
{
[SerializerPropertyName("p2")]
public int Property2 { get; set; }
}
var target = new A { Property1 = new B() };
// Access to the property by name
target.TrySetPropertyValueByPath("Property1.Property2", 123); // target.Property1.Property2 == 123
// Access to the property by alias
target.TrySetPropertyValueByPath("p1.p2", 123); // target.Property1.Property2 == 123
Using with dynamic instances:
var target = new DynamicDocument { { "Property1", new DynamicDocument() } };
target.TrySetPropertyValueByPath("Property1.Property2", 123); // target.Property1.Property2 == 123
Using with collection properties:
class C
{
[SerializerPropertyName("p1")]
public B[] Property1 { get; set; }
}
class B
{
[SerializerPropertyName("p2")]
public int Property2 { get; set; }
}
var target = new C { Property1 = new[] { new B() } };
// Access to the property by name
target.TrySetPropertyValueByPath("Property1.0.Property2", 123); // target.Property1[0].Property2 == 123
// Access to the property by alias
target.TrySetPropertyValueByPath("p1.0.p2", 123); // target.Property1[0].Property2 == 123