common/Utils/ClassAccess/ClassAccess.cs
2026-04-23 17:50:07 +02:00

120 lines
4.2 KiB
C#

using System;
using System.Reflection;
namespace Xylem.Common.Utils.ClassAccess
{
/// <summary>
/// Access to private methods of any class by System.Reflection
/// </summary>
public static class ClassAccess
{
/// <summary>
/// Access private or public methods by name
/// </summary>
/// <typeparam name="TInstance"></typeparam>
/// <typeparam name="TReturn"></typeparam>
/// <param name="classInstance">class reference</param>
/// <param name="methodName">method in class</param>
/// <param name="parameters">optional parameters</param>
/// <param name="isPrivateMethod"></param>
/// <returns></returns>
/// <remarks date="2024-11-07" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static TReturn CallMethode<TInstance, TReturn>(TInstance classInstance, String methodName,
Object[] parameters = null, Boolean isPrivateMethod = false)
{
var type = classInstance.GetType();
BindingFlags bindingFlags;
if (isPrivateMethod)
{
bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
}
else
{
bindingFlags = BindingFlags.Public | BindingFlags.Instance;
}
var method = type.GetMethod(methodName, bindingFlags);
if (method != null)
{
return (TReturn)method.Invoke(classInstance, parameters);
}
return default(TReturn);
}
/// <summary>
/// Getting the value of a property referenced by the name
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <typeparam name="TObj"></typeparam>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
/// <returns>false if property cannot be found or accessed</returns>
/// <remarks date="2024-11-07" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean GetPropertyValue<TType, TObj>(TObj obj, String propertyName, out TType value)
{
value = default(TType);
if (obj == null)
return false;
var property = typeof(TObj).GetProperty(propertyName);
if (property is null)
return false;
value = (TType)property.GetValue(obj, null);
return true;
}
/// <summary>
/// Setting the value of a property referenced by the name
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <typeparam name="TObj"></typeparam>
/// <param name="obj"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
/// <returns>false if property cannot be found or accessed</returns>
/// <remarks date="2024-11-07" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean SetPropertyValue<TType, TObj>(TObj obj, String propertyName, TType value)
{
if (value == null || obj == null)
return false;
var property = typeof(TObj).GetProperty(propertyName);
if (property is null || !property.CanWrite)
return false;
property.SetValue(obj, value);
return true;
}
/// <summary>
/// Copy all properties.
/// </summary>
/// <typeparam name="TObj"></typeparam>
/// <param name="destination"></param>
/// <param name="source"></param>
/// <returns>false if property cannot be found or accessed</returns>
/// <remarks date="2024-11-07" author="Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean CopyAllProperties<TObj>(TObj destination, TObj source)
{
var type = typeof(TObj);
foreach (var prop in type.GetProperties())
{
if (prop.CanWrite)
prop.SetValue(destination, prop.GetValue(source, null), null);
}
return true;
}
}
}