Skip to content

Commit

Permalink
Add quick predicate expression build method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Codespilot committed Sep 25, 2023
1 parent d88358c commit 3b30eaa
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 127 deletions.
117 changes: 117 additions & 0 deletions Source/Euonia.Core/Reflection/Reflect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,18 +397,33 @@ public static void SetValue(object obj, Type objectType, string propertyPath, ob
if (properties.Length == 1)
{
property = objectType.GetProperty(properties.First());
if (property == null)
{
throw new MissingMemberException($"Property {properties.First()} not found on type {objectType.FullName}.");
}

property.SetValue(obj, value);
return;
}

for (var i = 0; i < properties.Length - 1; i++)
{
property = currentType.GetProperty(properties[i]);
if (property == null)
{
throw new MissingMemberException($"Property {properties[i]} not found on type {currentType.FullName}.");
}

obj = property.GetValue(obj, null);
currentType = property.PropertyType;
}

property = currentType.GetProperty(properties.Last());
if (property == null)
{
throw new MissingMemberException($"Property {properties.Last()} not found on type {currentType.FullName}.");
}

property.SetValue(obj, value);
}

Expand Down Expand Up @@ -467,6 +482,108 @@ public static object InvokeGenericMethod(object obj, string methodName, Type[] g
var genericMethod = method.MakeGenericMethod(genericTypes);
return genericMethod.Invoke(method.IsStatic ? null : obj, parameters);
}

#region Public Static Methods

/// <summary>
/// Attempts to find the overloaded method that we want to call. Returns null if not found. This overload looks at the parameter types passed in vs method parameters off of the type we pass in
/// </summary>
/// <param name="methodNameToRetrieve">What is the method to name to find</param>
/// <param name="typeToLookThroughTheMethods">The type to retrieve the methods off of, so we can look through it and try to find the correct method</param>
/// <param name="methodParameterTypes">Look for the method parameter types in the method to match. If the method takes a string and an int, then we will look for that in every method</param>
/// <returns>Method info found, or null value if not found</returns>
public static MethodInfo FindMethod(string methodNameToRetrieve, Type typeToLookThroughTheMethods, params Type[] methodParameterTypes)
{
//going to use the overload. So we can create the func with calling the other method
return FindMethod(methodNameToRetrieve, typeToLookThroughTheMethods, x => MethodParameterSelector(x, methodParameterTypes));
}

/// <summary>
/// Attempts to find the overloaded method that we want to call. Returns null if not found. This method will try to evaluate the MethodSelect for each method and check to see if it returns true.
/// </summary>
/// <param name="methodNameToRetrieve">What is the method to name to find</param>
/// <param name="methodSelector">Gives the calling method the ability to look through the parameters and pick the correct method</param>
/// <param name="typeToLookThroughTheMethods">The type to retrieve the methods off of, so we can look through it and try to find the correct method</param>
/// <returns>Method info found, or null value if not found</returns>
public static MethodInfo FindMethod(string methodNameToRetrieve, Type typeToLookThroughTheMethods, Func<MethodInfo, bool> methodSelector)
{
//use the overload
return FindMethod(methodNameToRetrieve, typeToLookThroughTheMethods.GetMethods(), methodSelector);
}

/// <summary>
/// Attempts to find the overloaded method that we want to call. Returns null if not found. This method will try to evaluate the MethodSelect for each method and check to see if it returns true.
/// Call this method if you already have the method info's that match the same name you are looking for
/// </summary>
/// <param name="methodNameToRetrieve">What is the method to name to find</param>
/// <param name="methodSelector">Gives the calling method the ability to look through the parameters and pick the correct method</param>
/// <param name="methodsToLookThrough">Methods that have the same name. Or methods to loop through and inspect against the method selector.</param>
/// <returns>Method info found, or null value if not found</returns>
public static MethodInfo FindMethod(string methodNameToRetrieve, IEnumerable<MethodInfo> methodsToLookThrough, Func<MethodInfo, bool> methodSelector)
{
//let's start looping through the methods to see if we can find a match
return methodsToLookThrough.FirstOrDefault(methodToInspect => string.Equals(methodNameToRetrieve, methodToInspect.Name, StringComparison.OrdinalIgnoreCase) && methodSelector(methodToInspect));

//we never found a match, so just return null
}

#endregion

#region Private Static Methods

/// <summary>
/// Private helper method to look at the current method and inspect it for the method parameter types. If they match return true, else return false
/// </summary>
/// <param name="methodToEvaluate">Method to evaluate and check if we have a match based on the method parameter types</param>
/// <param name="methodParameterTypes"></param>
/// <returns>Do we have a match? Do the method parameter types match?</returns>
private static bool MethodParameterSelector(MethodBase methodToEvaluate, params Type[] methodParameterTypes)
{
//we are going to match the GetParameters and the MethodParameterTypes. It needs to match index for index and type for type. So GetParameters[0].Type must match MethodParameterTypes[0].Type...[1].Type must match [1].Type

//holds the index with the method parameter types we are up too
int i = 0;

//let's loop through the parameters
foreach (ParameterInfo thisParameter in methodToEvaluate.GetParameters())
{
//it's a generic parameter...ie...TSource then we are going to ignore it because whatever we pass in would be TSource
if (!thisParameter.ParameterType.IsGenericParameter)
{
//is this a generic type? we need to compare this differently
if (thisParameter.ParameterType.IsGenericType)
{
//is the method parameter a generic type?
if (!methodParameterTypes[i].IsGenericType)
{
//it isn't so return false..cause they aren't the same
return false;
}

//if the generic type's don't match then return false...This might be problematic...it works for the scenario which I'm using it for so we will leave this and modify afterwards
if (thisParameter.ParameterType.GetGenericTypeDefinition() != methodParameterTypes[i].GetGenericTypeDefinition())
{
//doesn't match return false
return false;
}
}
else if (thisParameter.ParameterType != methodParameterTypes[i].UnderlyingSystemType)
{
//this is a regular parameter so we can compare it normally
//we don't have a match...so return false
return false;
}
}

//increment the index
i++;
}

//if we get here then everything matches so return true
return true;
}

#endregion
}

/// <summary>
Expand Down
Loading

0 comments on commit 3b30eaa

Please sign in to comment.