From 06d7074622e0b4ad342482a0b1615364263d8123 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 29 May 2026 19:33:29 +0330 Subject: [PATCH] add some new Extensions method for Class Manipulations using Reflection ... --- Extensions/ModelExtensions.cs | 232 +++++++++++++++++++++++++++++++++- 1 file changed, 231 insertions(+), 1 deletion(-) diff --git a/Extensions/ModelExtensions.cs b/Extensions/ModelExtensions.cs index 56b34f8..aa3a981 100644 --- a/Extensions/ModelExtensions.cs +++ b/Extensions/ModelExtensions.cs @@ -3,11 +3,22 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; +using System.Reflection; namespace xCommons.Extensions { public static partial class ModelExtensions { + /// + /// Retrieve a Binding Flag for Retrieve all Instance Members including + /// Priate and Public ... + /// + /// + public static BindingFlags GetDefaultBindingFlag() + { + return BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + } + /// /// Update an Instance of a Typed Object with Data Provided by another Instance of Typed Object /// @@ -122,6 +133,144 @@ namespace xCommons.Extensions return result; } + /// + /// Check a Class is Contains a Filed or not ... + /// + /// + /// + /// + /// + public static bool HasField( + this T source, + string filed + ) + where T : class + { + // + var result = source + .GetType() + .GetFields() + .Any(fld => fld.Name == filed); + + // + return result; + } + + /// + /// Check a Class is Contains a Method or not ... + /// + /// + /// + /// + /// + public static bool HasMethod( + this T source, + string method + ) + where T : class + { + // + var result = source + .GetType() + .GetMethods() + .Any(mthd => mthd.Name == method); + + // + return result; + } + + /// + /// Retrieve a Class Properties Names ... + /// + /// + /// + /// + /// + public static string[] GetPropertiesNames( + this T source, + BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic + ) + { + // + var result = source + .GetType() + .GetProperties(bindingAttr) + .Select(p => p.Name) + .ToArray(); + + // + return result; + } + + /// + /// Retrieve a Class Fields Names ... + /// + /// + /// + /// + /// + public static string[] GetFiledsNames( + this T source, + BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic + ) + { + // + var result = source + .GetType() + .GetFields(bindingAttr) + .Select(p => p.Name) + .ToArray(); + + // + return result; + } + + /// + /// Retrieve a Class Methods Names ... + /// + /// + /// + /// + /// + public static string[] GetMethodsNames( + this T source, + BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic + ) + { + // + var result = source + .GetType() + .GetMethods(bindingAttr) + .Select(p => p.Name) + .ToArray(); + + // + return result; + } + + /// + /// Retrieve a Class Members Names ... + /// + /// + /// + /// + /// + public static string[] GetMembersNames( + this T source, + BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic + ) + { + // + var result = source + .GetType() + .GetMembers(bindingAttr) + .Select(p => p.Name) + .ToArray(); + + // + return result; + } + /// /// Get a Class Property Value using Generic Concept ... /// @@ -162,7 +311,41 @@ namespace xCommons.Extensions } /// - /// Setting Property Value of T ... + /// Get a Class Field Value using Generic Concept ... + /// + /// + /// + /// + /// + /// + public static TField GetField( + this T source, + string filed + ) + where T : class + { + // + TField result = default; + + // + if (source.HasField(filed)) + { + // + var fld = source + .GetType() + .GetField(filed); + + // + var fieldVal = fld.GetValue(source); + result = (TField)fieldVal; + } + + // + return result; + } + + /// + /// Set a Class Property Value using Generic Concept ... /// /// /// @@ -208,6 +391,53 @@ namespace xCommons.Extensions return result; } + /// + /// Set a Class Field Value using Generic Concept ... + /// + /// + /// + /// + /// + /// + /// + public static bool SetField( + this T source, + string filed, + TField value + ) + where T : class + { + // + // Check T Has Field ... + var result = source.HasField(filed); + if (!result) + { + return result; + } + + // + // Access Field ... + var fld = source + .GetType() + .GetField(filed); + + // + // Set Value ... + try + { + // + result = true; + fld.SetValue(source, value); + } + catch + { + result = false; + } + + // + return result; + } + /// /// Check two Instance of Typed Object Values are Same or not ///