C# Getting Value of Params Using Reflection

C# Getting value of params using reflection

You can hack your way around this by creating an anonymous type inside your method and taking advantage of projection initialisers. You can then interrogate the anonymous type's properties using reflection. For example:

static void ManyParms(
string a, string b, string c, int d, short e, bool f, string g)
{
var hack = new { a, b, c, d, e, f, g };

foreach (PropertyInfo pi in hack.GetType().GetProperties())
{
Console.WriteLine("{0}: {1}", pi.Name, pi.GetValue(hack, null));
}
}

Using reflection to get method name and parameters

What you're looking for is an interceptor. Like the name says, an interceptor intercepts a method invocation and allows you to perform things before and after a method is called. This is quite popular in many caching and logging frameworks.

How to get a method's parameter values from within the method using reflection?

Getting the method name is easy via [CallerMemberName] on a helper function like:

static string Me([CallerMemberName]string caller=null) => caller;

Getting the MethodInfo, and thus the ParameterInfo definitions (including the names) is pretty easy, but not very efficient. Getting the parameter values is not a thing you can do with the reflection API, and even if it could: the performance would be catastrophically bad. Since you say

and most importantly, the values passed into the function via the parameters.

I think you need to look at alternatives. One of which might just be "add the code you need manually". IL-weaving at build-time might be an option, but that's a much more advanced topic.

Get property value from string using reflection

 public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}

Of course, you will want to add validation and whatnot, but that is the gist of it.

Get method with an 'in' parameter through reflection

You should use MakeByRefType:

Console.WriteLine(
typeof(Foo).GetMethod("Bar", new[] { typeof(int).MakeByRefType() })
);

// prints "Void Bar(Int32 ByRef)"

How to pass an argument by reference using Reflection

If you had T, everything would be very simple:

// declare a delegate
private delegate T ReadDelegate(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options);

// create and invoke a delegate
var readDelegate = Delegate.CreateDelegate(typeof(ReadDelegate), converter, "Read") as ReadDelegate;
var result = readDelegate.Invoke(ref reader, type, options);

Source: second search result from Google

But since you haven't, things go much more fun. Here is my solution (not as clean as I would like, but it works). First you need a supplementary classes:

internal abstract class ReadHelper
{
public abstract object Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options);
}

internal class ReadHelper<T> : ReadHelper
{
private readonly ReadDelegate _readDelegate;

private delegate T ReadDelegate(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options);

public Reader(object converter)
{
_readDelegate = Delegate.CreateDelegate(typeof(ReadDelegate), converter, "Read") as ReadDelegate;
}

public override object Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
=> _readDelegate.Invoke(ref reader, type, options);
}

And now you can use them as this:

// I assume you know how to get this:
// var converter = ...
// var typeOfT = ...
// var reader = ...
// var type = ...
// var options = ...

var readHelperType = typeof(ReadHelper<>).MakeGenericType(typeOfT);
var readHelper = Activator.CreateInstance(readerType, converter) as Reader;

// and finally:
var result = readHelper.Read(ref reader, type, options);

And just in case, you don't know how to get typeOfT:

private Type FindTypeOfT(object converter)
{
var type = converter.GetType();
while (type != null)
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(JsonConverter<>))
return type.GetGenericArguments()[0];
else
type = type.BaseType;

return null;
}


Related Topics



Leave a reply



Submit