FlexonHelper (C#) Documentation
Overview
The C# implementation of FlexonHelper provides flexible object manipulation and type conversion utilities.
Installation
No external dependencies required. Uses standard .NET libraries.
Basic Usage
using Helpers;
// Get property value
var value = FlexonHelper.GetPropertyValue(obj, "PropertyName");
// Set property value
FlexonHelper.SetPropertyValue(obj, "PropertyName", newValue);
// Convert type
var number = FlexonHelper.Convert("123");
Core Features
Property Access
// Get nested property
var value = FlexonHelper.GetNestedPropertyValue(obj, "Parent.Child.Property");
// Set nested property
FlexonHelper.SetNestedPropertyValue(obj, "Parent.Child.Property", value);
// Check property exists
bool exists = FlexonHelper.HasProperty(obj, "PropertyName");
Type Conversion
// Basic conversion
int number = FlexonHelper.Convert("123");
DateTime date = FlexonHelper.Convert("2025-02-05");
// Safe conversion with default
int result = FlexonHelper.ConvertOrDefault("invalid", 0);
// Try conversion
if (FlexonHelper.TryConvert("123", out var parsed))
{
Console.WriteLine(parsed);
}
Collection Handling
// Convert to list
var list = FlexonHelper.ToList(enumerable);
// Convert to array
var array = FlexonHelper.ToArray(enumerable);
// Convert to dictionary
var dict = FlexonHelper.ToDictionary(obj);
Object Manipulation
// Clone object
var clone = FlexonHelper.Clone(sourceObj);
// Merge objects
var merged = FlexonHelper.Merge(obj1, obj2);
// Map properties
FlexonHelper.MapProperties(source, target);
Error Handling
try
{
var value = FlexonHelper.Convert("invalid");
}
catch (FlexonException ex)
{
Console.WriteLine($"Conversion error: {ex.Message}");
}
Advanced Usage
Custom Type Conversion
public class CustomConverter : ITypeConverter
{
public object Convert(object value, Type targetType)
{
// Custom conversion logic
return converted;
}
}
FlexonHelper.RegisterConverter(new CustomConverter());
Dynamic Property Access
public class PropertyAccessor
{
private readonly object _target;
private readonly PropertyInfo _property;
public PropertyAccessor(object target, string propertyName)
{
_target = target;
_property = target.GetType().GetProperty(propertyName);
}
public object GetValue()
{
return FlexonHelper.GetPropertyValue(_target, _property);
}
public void SetValue(object value)
{
FlexonHelper.SetPropertyValue(_target, _property, value);
}
}
Best Practices
- Type Safety
public static class TypeSafeFlexon
{
public static T GetPropertyValueSafe(object obj, string propertyName)
{
var value = FlexonHelper.GetPropertyValue(obj, propertyName);
return FlexonHelper.Convert(value);
}
}
- Caching Property Info
public class CachedPropertyAccessor
{
private static readonly ConcurrentDictionary Cache
= new ConcurrentDictionary();
public static object GetValue(object obj, string propertyName)
{
var prop = Cache.GetOrAdd(propertyName, name =>
obj.GetType().GetProperty(name));
return prop?.GetValue(obj);
}
}
- Validation
public static class ValidatedFlexon
{
public static void SetPropertyValueSafe(object obj, string propertyName, object value)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Property name cannot be empty");
FlexonHelper.SetPropertyValue(obj, propertyName, value);
}
}
Performance Optimization
- Compiled Expression Trees
public class CompiledPropertyAccessor
{
private static readonly ConcurrentDictionary> GetterCache
= new ConcurrentDictionary>();
public static object GetValue(T obj, string propertyName)
{
var getter = GetterCache.GetOrAdd(propertyName, name =>
{
var param = Expression.Parameter(typeof(T));
var prop = Expression.Property(param, name);
return Expression.Lambda>(
Expression.Convert(prop, typeof(object)),
param
).Compile();
});
return getter(obj);
}
}
- Bulk Operations
public static class BulkFlexon
{
public static void MapBulkProperties(IEnumerable sources, IEnumerable targets)
{
var properties = typeof(T).GetProperties();
var sourceList = sources.ToList();
var targetList = targets.ToList();
for (int i = 0; i < sourceList.Count; i++)
{
foreach (var prop in properties)
{
var value = prop.GetValue(sourceList[i]);
prop.SetValue(targetList[i], value);
}
}
}
}
Testing
[TestClass]
public class FlexonHelperTests
{
private class TestClass
{
public string StringProp { get; set; }
public int IntProp { get; set; }
}
[TestMethod]
public void GetPropertyValue_ValidProperty_ReturnsValue()
{
// Arrange
var obj = new TestClass { StringProp = "test" };
// Act
var value = FlexonHelper.GetPropertyValue(obj, "StringProp");
// Assert
Assert.AreEqual("test", value);
}
[TestMethod]
public void Convert_ValidString_ReturnsInt()
{
// Arrange
var input = "123";
// Act
var result = FlexonHelper.Convert(input);
// Assert
Assert.AreEqual(123, result);
}
}
Legal Disclaimer
This documentation and associated helper scripts are provided "as is" without warranty of any kind, either express or implied.
- The code examples and helper functions are for illustrative purposes only.
- Users should thoroughly test any implementation in their specific environment.
- The authors are not responsible for any issues or damages arising from the use of these scripts.
- Always follow security best practices and your organization's coding guidelines.