Skip to content

Commit

Permalink
Align code formatting (open code block bracket on the same line, tabs)
Browse files Browse the repository at this point in the history
  • Loading branch information
VitaliyMF committed Dec 31, 2023
1 parent 7d3f2c8 commit b94b6b1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 52 deletions.
32 changes: 15 additions & 17 deletions src/NReco.LambdaParser/Linq/IInvokeMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@
using System.Collections.Generic;
using System.Text;

namespace NReco.Linq
{
namespace NReco.Linq {

/// <summary>
/// Exposes a method that allows the invoke of a method within an object
/// </summary>
/// <remarks>
/// Interface to allow different implimentations of invoke method with different capabilities.
/// ensures backwards compatibility and behavour.
/// </remarks>
public interface IInvokeMethod
{
/// <summary>
/// Exposes a method that allows the invoke of a method within an object
/// </summary>
/// <remarks>
/// Interface to allow different implimentations of invoke method with different capabilities.
/// ensures backwards compatibility and behavour.
/// </remarks>
public interface IInvokeMethod {

/// <summary>
/// Invokes a method within an object (targetobject), given a set of arguments / parameters passed to method
/// </summary>
/// <returns>An object reference to the return value of the method</returns>
object Invoke(object TargetObject, string MethodName, object[] args);
/// <summary>
/// Invokes a method within an object (targetobject), given a set of arguments / parameters passed to method
/// </summary>
/// <returns>An object reference to the return value of the method</returns>
object Invoke(object targetObject, string methodName, object[] args);

}
}
}
6 changes: 3 additions & 3 deletions src/NReco.LambdaParser/Linq/ILambdaValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
using System.Text;
using System.Threading.Tasks;

namespace NReco.Linq
{
namespace NReco.Linq {

/// <summary>
/// Represents a value in expressions produced by <see cref="LambdaParser"/>.
/// </summary>
public interface ILambdaValue {
object Value { get; }
}
}

}
6 changes: 3 additions & 3 deletions src/NReco.LambdaParser/Linq/IValueComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
using System.Text;
using System.Threading.Tasks;

namespace NReco.Linq
{
namespace NReco.Linq {

/// <summary>
/// Exposes a method that compares two objects.
/// </summary>
Expand All @@ -34,6 +34,6 @@ public interface IValueComparer {
/// </summary>
/// <returns>A signed integer that indicates the relative values of x and y or null if values cannot be compared.</returns>
int? Compare(object x, object y);
}
}

}
24 changes: 12 additions & 12 deletions src/NReco.LambdaParser/Linq/LambdaParameterWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {

var delegParams =
#if NET40
deleg.Method.GetParameters();
deleg.Method.GetParameters();
#else
deleg.GetMethodInfo().GetParameters();
deleg.GetMethodInfo().GetParameters();
#endif
if (delegParams.Length != args.Length)
throw new TargetParameterCountException(
Expand All @@ -120,7 +120,7 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {
for (int i = 0; i < resolvedArgs.Length; i++) {
var argObj = args[i] is LambdaParameterWrapper ? ((LambdaParameterWrapper)args[i]).Value : args[i];
//Static method call below, what should we do with this? Perhaps we should move it somewhere else
if (!NReco.OptionsParamsInvokeMethod.IsInstanceOfType(delegParams[i].ParameterType, argObj))
if (!NReco.InvokeMethod.IsInstanceOfType(delegParams[i].ParameterType, argObj))
argObj = Convert.ChangeType(argObj, delegParams[i].ParameterType, CultureInfo.InvariantCulture);
resolvedArgs[i] = argObj;
}
Expand All @@ -134,33 +134,33 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {
obj = ((LambdaParameterWrapper)obj).Value;

//Additional check since obj appears to still be null in some use cases
if (obj == null)
throw new NullReferenceException(String.Format("Property or field {0} target is null", propertyName));
if (obj == null)
throw new NullReferenceException(String.Format("Property or field {0} target is null", propertyName));


PropertyInfo prop;
try
{
PropertyInfo prop;
try
{
#if NET40
prop = obj.GetType().GetProperty(propertyName);
#else
prop = obj.GetType().GetRuntimeProperty(propertyName);
#endif
}
}
//Below covers an issue caused by properties declared in base classes with different signitures
//in these cases an AmbiguousMatchException is thrown
//if this happens then we look for the first match by propertyName since this
//seems to be the one from the decendant class.
catch (System.Reflection.AmbiguousMatchException)
{
{
#if NET40
prop = obj.GetType().GetProperties().FirstOrDefault(rp=>rp.Name == propertyName);
#else
prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(rp=>rp.Name == propertyName);
#endif
}
}

if (prop != null) {
if (prop != null) {
var propVal = prop.GetValue(obj, null);
return new LambdaParameterWrapper(propVal, Cmp, Inv);
}
Expand Down
1 change: 0 additions & 1 deletion src/NReco.LambdaParser/Linq/LambdaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using System.ComponentModel;
using System.Globalization;


namespace NReco.Linq {

/// <summary>
Expand Down
27 changes: 11 additions & 16 deletions src/NReco.LambdaParser/OptionsParamsInvokeMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
using System.Text;
using System.Reflection;

namespace NReco {
namespace NReco.Linq {

/// <summary>
/// Invoke object's method that is most compatible with provided arguments
/// </summary>
public class OptionsParamsInvokeMethod : Linq.IInvokeMethod {
public class OptionsParamsInvokeMethod : IInvokeMethod {

internal readonly static OptionsParamsInvokeMethod _Instance = new OptionsParamsInvokeMethod();
public static Linq.IInvokeMethod Instance
{
get {
return _Instance;
}
}
public static IInvokeMethod Instance => _Instance;

protected MethodInfo FindMethod(object TargetObject, string MethodName, Type[] argTypes) {
if (TargetObject is Type) {
Expand Down Expand Up @@ -74,18 +69,18 @@ public static Linq.IInvokeMethod Instance
}


public object Invoke(object TargetObject, string MethodName, object[] args) {
public object Invoke(object targetObject, string methodName, object[] args) {
Type[] argTypes = new Type[args.Length];
for (int i = 0; i < argTypes.Length; i++)
argTypes[i] = args[i] != null ? args[i].GetType() : typeof(object);

// strict matching first
MethodInfo targetMethodInfo = FindMethod(TargetObject, MethodName, argTypes);
MethodInfo targetMethodInfo = FindMethod(targetObject, methodName, argTypes);
// fuzzy matching
if (targetMethodInfo==null) {
var methods = GetAllMethods(TargetObject);
var methods = GetAllMethods(targetObject);
foreach (var m in methods) {
if (m.Name == MethodName) {
if (m.Name == methodName) {
var para = m.GetParameters();
var paracnt = para.Length;
var optcnt = OptionalParameterCount(para);
Expand All @@ -104,12 +99,12 @@ public static Linq.IInvokeMethod Instance
argTypeNames[i] = argTypes[i].Name;
string argTypeNamesStr = String.Join(",",argTypeNames);
throw new MissingMemberException(
(TargetObject is Type ? (Type)TargetObject : TargetObject.GetType()).FullName+"."+MethodName);
(targetObject is Type ? (Type)targetObject : targetObject.GetType()).FullName+"."+methodName);
}
object[] argValues = PrepareActualValues(MethodName,targetMethodInfo.GetParameters(),args);
object[] argValues = PrepareActualValues(methodName,targetMethodInfo.GetParameters(),args);
object res = null;
try {
res = targetMethodInfo.Invoke( TargetObject is Type ? null : TargetObject, argValues);
res = targetMethodInfo.Invoke( targetObject is Type ? null : targetObject, argValues);
} catch (TargetInvocationException tiEx) {
if (tiEx.InnerException!=null)
throw new Exception(tiEx.InnerException.Message, tiEx.InnerException);
Expand Down Expand Up @@ -170,7 +165,7 @@ private bool CheckParamValueCompatibility(Type paramType, object val)
else {
if (!CheckParamValueCompatibility(paramType, values[i])) return false;
}
}
}
else {
if (!paramsInfo[i].IsOptional) return false;
}
Expand Down

0 comments on commit b94b6b1

Please sign in to comment.