add some new Extensions method for Class Manipulations using Reflection ...
This commit is contained in:
@@ -3,11 +3,22 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace xCommons.Extensions
|
namespace xCommons.Extensions
|
||||||
{
|
{
|
||||||
public static partial class ModelExtensions
|
public static partial class ModelExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a Binding Flag for Retrieve all Instance Members including
|
||||||
|
/// Priate and Public ...
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static BindingFlags GetDefaultBindingFlag()
|
||||||
|
{
|
||||||
|
return BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Update an Instance of a Typed Object with Data Provided by another Instance of Typed Object
|
/// Update an Instance of a Typed Object with Data Provided by another Instance of Typed Object
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -122,6 +133,144 @@ namespace xCommons.Extensions
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check a Class is Contains a Filed or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="filed"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool HasField<T>(
|
||||||
|
this T source,
|
||||||
|
string filed
|
||||||
|
)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetFields()
|
||||||
|
.Any(fld => fld.Name == filed);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check a Class is Contains a Method or not ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="method"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool HasMethod<T>(
|
||||||
|
this T source,
|
||||||
|
string method
|
||||||
|
)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetMethods()
|
||||||
|
.Any(mthd => mthd.Name == method);
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a Class Properties Names ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="bindingAttr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string[] GetPropertiesNames<T>(
|
||||||
|
this T source,
|
||||||
|
BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetProperties(bindingAttr)
|
||||||
|
.Select(p => p.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a Class Fields Names ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="bindingAttr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string[] GetFiledsNames<T>(
|
||||||
|
this T source,
|
||||||
|
BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetFields(bindingAttr)
|
||||||
|
.Select(p => p.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a Class Methods Names ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="bindingAttr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string[] GetMethodsNames<T>(
|
||||||
|
this T source,
|
||||||
|
BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetMethods(bindingAttr)
|
||||||
|
.Select(p => p.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve a Class Members Names ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="bindingAttr"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string[] GetMembersNames<T>(
|
||||||
|
this T source,
|
||||||
|
BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = source
|
||||||
|
.GetType()
|
||||||
|
.GetMembers(bindingAttr)
|
||||||
|
.Select(p => p.Name)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get a Class Property Value using Generic Concept ...
|
/// Get a Class Property Value using Generic Concept ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -162,7 +311,41 @@ namespace xCommons.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Setting Property Value of T ...
|
/// Get a Class Field Value using Generic Concept ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <typeparam name="TField"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="filed"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static TField GetField<T, TField>(
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a Class Property Value using Generic Concept ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <typeparam name="TProp"></typeparam>
|
/// <typeparam name="TProp"></typeparam>
|
||||||
@@ -208,6 +391,53 @@ namespace xCommons.Extensions
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set a Class Field Value using Generic Concept ...
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <typeparam name="TField"></typeparam>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="filed"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool SetField<T, TField>(
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Check two Instance of Typed Object Values are Same or not
|
/// Check two Instance of Typed Object Values are Same or not
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user