I have a type variable
using System;
using System.Linq;
using System.Reflection;
...
var validateFuncType = typeof(Func<,>).MakeGenericType(someVariableType, typeof(bool));
Now I check if someVariableType follows a convention,
var validateOfType = someVariableType
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.SingleOrDefault(mi =>
{
if (mi.Name != "Validate" || mi.ReturnType != typeof(bool))
{
return false;
}
var parameters = mi.GetParameters();
return parameters.Length == 0;
});
then depending on the check
object validateFunc;
if (validateOfType == null)
{
validateFunc = // some noop func that returns true.
// e.g. _ => true;
}
else
{
validateFunc = // a delegate that calls the conventional validate
// e.g. someVariable => someVariable.Validate();
}
instantiate an instance of the delegate type.
Can you help me do that, how can I instantiate validateFuncType, that calls the conventional implementation, if it exists?
If I understand correctly, you are looking for Delegate.CreateDelegate:
var alwaysReturnTrueMethodInfo = typeof(YourClass).GetMethod("AlwaysReturnTrue").MakeGenericMethod(someVariableType);
Delegate validateFunc;
if (validateOfType == null)
{
validateFunc = Delegate.CreateDelegate(validateFuncType, alwaysReturnTrueMethodInfo);
}
else
{
validateFunc = Delegate.CreateDelegate(validateFuncType, validateOfType);
}
where AlwaysReturnTrue is a helper static method declared like this:
public static bool AlwaysReturnTrue<T>(T t) { return true }
You can do it either by:
// create an object of the type
var obj = Activator.CreateInstance(validateFuncType);
And you'll get an instance of validateFuncType in obj.
Another way is to use reflection:
// get public constructors
var ctors = validateFuncType.GetConstructors(BindingFlags.Public);
// invoke the first public constructor with no parameters.
var obj = ctors[0].Invoke(new object[] { });
This was taken from this SO answer. And because of that this question (and answer) might be marked as duplicate.
What I did, after noticing that input parameters for Func<> are contravariant.
object validateFunc = validateOfType != null
? config => (bool)validateOfType.Invoke(config, new object[0])
: new Func<object, bool>(_ => true);
I'm not sure if this is better than #Sweeper's answer
Related
I have an object that provides several functions to write and read data from a packet, something like this:
class Packet
{
void Write(int value) {/*...*/}
//...
int ReadInt() {/*...*/}
bool ReadBool() {/*...*/}
string ReadString() {/*...*/}
Vector3 ReadVector3() {/*...*/}
}
this class stores a byte[] that is sent over the network. If I want to access data previously written, I can do it like this
void MyFunction(Packet packet)
{
int myInt = packet.ReadInt();
bool myBool = packet.ReadBool();
string myString = packet.ReadString();
Vector3 myVector3 = packet.ReadVector3();
//... do stuff
}
I was wondering if there is some syntactic sugar that would allow me to define a function taking a variable number of parameters of different types, detect which dynamic types they are at runtime, call the appropriate function and then return the parameter, something like this:
class Packet
{
//...
void ReadAll(out params object[] objects);
}
void MyFunction(Packet packet)
{
packet.ReadAll(out int myInt, out bool myBool, out string myString, out Vector3 myVector3);
//... do stuff with myInt, myBool, myString, myVector3
}
I looked at params with an object[], the out keyword, generics, and Convert.ChangeType() but I couldn't get anything to work so far. I'm not sure this is even possible, and if it is, if the runtime cost of reflection will highly outweigh the benefits of simpler/less code for something used as frequently as network packets.
Thank you all.
You can try using generics and ValueTuples:
public T Read<T>() where T:ITuple
{
return default(T); // some magic to create and fill one
}
and usage:
var (i, j) = Read<(int, int)>();
// or
var x = Read<(int i, int j)>();
As for reflection - you can cache reflection "results" per type:
public T Read<T>() where T : struct, ITuple
{
return TupleCreator<T>.Create(new ValueReader());
}
static class TupleCreator<T> where T : struct, ITuple
{
private static Func<ValueReader, T> factory;
static TupleCreator()
{
var fieldTypes = typeof(T).GetFields()
.Select(fi => fi.FieldType)
.ToArray();
if(fieldTypes.Length > 7)
{
throw new Exception("TODO");
}
var createMethod = typeof(ValueTuple).GetMethods()
.Where(m => m.Name == "Create" && m.GetParameters().Length == fieldTypes.Length)
.SingleOrDefault() ?? throw new NotSupportedException("ValueTuple.Create method not found.");
var createGenericMethod = createMethod.MakeGenericMethod(fieldTypes);
var par = Expression.Parameter(typeof(ValueReader));
// you will need to figure out how to find your `read` methods
// but it should be easy - just find a method starting with "Read"
// And returning desired type
// I can do as simple as:
var readMethod = typeof(ValueReader).GetMethod("Read");
var readCalls = fieldTypes
.Select(t => readMethod.MakeGenericMethod(t)) // can cache here also but I think not needed to
.Select(m => Expression.Call(par, m));
var create = Expression.Call(createGenericMethod, readCalls);
factory = Expression.Lambda<Func<ValueReader, T>>(create, par).Compile();
}
public static T Create(ValueReader reader) => factory(reader);
}
class ValueReader
{
public T Read<T>()
{
return default; // to simulate your read
}
}
Console.WriteLine(Read<(int i, double j)>()); // prints "(0, 0)"
NB
Also you can just implement the Read<T> method with "caching" the reflection as done here.
Yes, Reflection is always having a cost of Performance, but it's not always so big, especially when you are not using reflective calls via big size collections or dealing with a very complicated code (here is a Microsoft official tutorial about when to use the Reflection).
Anyway, back to our business, using a reflection here is what I think might help you:
class Packet {
...
public T Read<T>()
{
var currentType = Type.GetType(this.GetType().ToString());
var methodInfo = currentType?
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) // Getting all "private" read methods
.FirstOrDefault(m =>
m.Name.Contains("Read") && m.ReturnType == typeof(T));
// here it is assuming that you will have to have only 1 Read method with the type int, bool, etc.
return (T) methodInfo?.Invoke(this, null);
}
}
public List<object> ReadAllAsCollection(params Type[] types)
{
var result = new List<object>(); // in order to hold various type you need to have a collection of `object` type elements
foreach (Type type in types)
{
MethodInfo method = typeof(Packet).GetMethod("Read");
MethodInfo genericMethod = method?.MakeGenericMethod(type);
var res = genericMethod?.Invoke(this, null); // calling Read<T> with appropriate type
result.Add(res);
}
return result;
}
public T ReadAllAsTuple<T>() where T : ITuple
{
T CreateValueTuple(List<object> items, Type[] inputTypes)
{
var methodInfo = typeof(ValueTuple)
.GetMethods()
.FirstOrDefault(m =>
m.Name.Contains("Create") &&
m.GetParameters().Length == items.Count);
var invokeResult = methodInfo?.MakeGenericMethod(inputTypes)
.Invoke(null, items.ToArray());
return (T)invokeResult;
}
var tupleType = typeof(T);
var types = tupleType.GetFields().Select(f => f.FieldType).ToArray();
var result = types
.Select(t =>
{
var method = typeof(Packet).GetMethod("Read");
var genericMethod = method?.MakeGenericMethod(t);
return genericMethod?.Invoke(this, null); // calling Read<T> with appropriate type
}).ToList();
return result.Any() ?
CreateValueTuple(result, types)
: default;
}
And a usage:
var p = new Packet();
var elm = p.Read<int>(); // single instance call, ReadInt() call is encapsulated
var resultAsCollection = p.ReadAllAsCollection(typeof(int), typeof(string));
var resultAsTuple = p.ReadAllAsTuple<ValueTuple<int, string, bool>>();
A few notes:
As you probably want a single exposed public method (Read), it would be good to have other methods declared as private.
In this case, you have simple method names, but to escape possible name conflicts, you can use some specific naming conventions, like: _ReadInt_() or any other namings you prefer. Though, this example will work even without specific namings.
It is assumed that you will have only one method with the name Read... and return type of T (for example int ReadInt()) because, for the above case, we are using only the first match.
I have this type that contains two overloads of a generic method. I like to retrieve one of the overloads (with the Func<T> parameter) using reflection. The problem however is that I can't find the correct parameter type to supply the Type.GetMethod(string, Type[]) method with.
Here is my class definition:
public class Foo
{
public void Bar<T>(Func<T> f) { }
public void Bar<T>(Action<T> a) { }
}
And this is what I've come up with, unfortunately without succes:
[TestMethod]
public void Test1()
{
Type parameterType = typeof(Func<>);
var method = typeof(Foo).GetMethod("Bar", new Type[] { parameterType });
Assert.IsNotNull(method); // Fails
}
How can I get the MethodInfo of a generic method of which I know the parameters?
Why don't you use expression trees? This makes it much easier:
public static MethodInfo GetMethod<T>(
Expression<Action<T>> methodSelector)
{
var body = (MethodCallExpression)methodSelector.Body;
return body.Method;
}
[TestMethod]
public void Test1()
{
var expectedMethod = typeof(Foo)
.GetMethod("Bar", new Type[] { typeof(Func<>) });
var actualMethod =
GetMethod<Foo>(foo => foo.Bar<object>((Func<object>)null)
.GetGenericMethodDefinition();
Assert.AreEqual(expectedMethod, actualMethod);
}
Surprisingly, it looks like you'll need to call GetMethods() and loop over the methods until you find the one you want.
For example:
var yourMethod = typeof(Foo).GetMethods()
.First(m => m.Name == "Bar"
&& m.GetParameters().Length == 1
&& m.GetParameters()[0].ParameterType.ContainsGenericParameters
&& m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(Func<>));
I don't think you can do this directly using GetMethod. I suspect you'll have to iterate over all the methods called Bar, then:
Check that the method has one type parameter
Check that the method has one normal parameter
Use the type parameter to make a Func<T> (with typeof(Func<>).MakeGenericType) and check that the parameter type matches that.
LINQ is good for this sort of thing. Complete sample:
using System;
using System.Reflection;
using System.Linq;
public class Foo
{
public void Bar<T>(Func<T> f) { }
public void Bar<T>(Action<T> a) { }
}
class Test
{
static void Main()
{
var methods = from method in typeof(Foo).GetMethods()
where method.Name == "Bar"
let typeArgs = method.GetGenericArguments()
where typeArgs.Length == 1
let parameters = method.GetParameters()
where parameters.Length == 1
where parameters[0].ParameterType ==
typeof(Func<>).MakeGenericType(typeArgs[0])
select method;
Console.WriteLine("Matching methods...");
foreach (var method in methods)
{
Console.WriteLine(method);
}
}
}
Basically generics and reflection are really nasty in combination, I'm afraid :(
You need to specify a concrete type using MethodInfo.MakeGenericMethod.
However, I should point out, that getting the right type to invoke MakeGenericMethod on is not easy when you have an overloaded generic method.
Here is an example:
var method = typeof(Foo)
.GetMethods()
.Where(x => x.Name == "Bar")
.Where(x => x.IsGenericMethod)
.Where(x => x.GetGenericArguments().Length == 1)
.Where(x => x.GetParameters().Length == 1)
.Where(x =>
x.GetParameters()[0].ParameterType ==
typeof(Action<>).MakeGenericType(x.GetGenericArguments()[0])
)
.Single();
method = method.MakeGenericMethod(new Type[] { typeof(int) });
Foo foo = new Foo();
method.Invoke(foo, new Func<int>[] { () => return 42; });
You'll struggle with just with just GetMethod - you could try something along the lines of;
var method = (from m in typeof(Foo).GetMethods()
where
m.IsGenericMethodDefinition == true &&
m.Name == "Bar" &&
m.GetParameters().Length > 0 &&
m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == parameterType
select m).FirstOrDefault();
How do you determine if a parameter has a custom attribute attached to it?
I thought this test case would pass:
[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
var result = false;
foreach (var attribute in theString.GetType().GetCustomAttributes(true))
{
if (attribute.GetType() == (typeof(NotRequiredAttribute)))
{
result = true;
}
}
Assert.That(result, Is.True);
}
It requires a little bit more work.
[TestCase("")]
public void TestParameterAttribute([NotRequired]string theString)
{
var method = MethodInfo.GetCurrentMethod();
var parameter = method.GetParameters()[0];
var result = false;
foreach (var attribute in parameter.GetCustomAttributes(true))
{
if (attribute.GetType() == (typeof(NotRequiredAttribute)))
{
result = true;
}
}
Assert.That(result, Is.True);
}
theString.GetType() gets a reference to the Type representing a string. Calling GetCustomAttributes on it will look in the string class for those attributes.
What you want to do.. is get the attributes for the parameters in the current method. Maybe something like this:
var result = false;
foreach (var parameter in MethodInfo.GetCurrentMethod().GetParameters())
{
if (parameter.GetCustomAttributes().Any(x => x.GetType() == typeof (NotRequiredAttribute)))
result = true;
}
Also you can use Generic version of GetCustomAttribute method:
parameter.GetCustomAttribute<NotRequiredAttribute>() != null
I have a small dependency injection framework, and I am trying to make it resolve Lazy<> instances dynamically. The idea is to do something like that:
DIContainer.Register<IDbCommand,SqlCommand>();
var lazyCommand = DIContainer.Resolve<Lazy<IDbCommand>>();
I read the other day that Autofac was able of doing that.
I am stuck trying to set the constructor for that Lazy<> instance. In the next test code, a exception is thrown because the desired type constructor is expecting a Func<arg>, but I am passing a Func<Object>:
static readonly Type _lazyType = typeof(Lazy<>);
static Object ResolveTest(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
{
var arg = type.GetGenericArguments()[0];
return Activator.CreateInstance(_lazyType.MakeGenericType(arg), new Func<Object>(() => ResolveType(arg)));
}
else
return ResolveType(type);
}
I am out of ideas about how to create a delegate that fits for the Lazy<> constructor parameter. Any idea?
Cheers.
That's not trivial. One possible solution would be to work with reflection:
Create a generic ResolveType method:
public static T ResolveType<T>()
{
return (T)ResolveType(typeof(T));
}
Create a delegate that uses this method:
// You probably want to cache this MethodInfo:
var method = typeof(TypeContainingResolveType)
.GetMethods()
.Single(x => x.IsGenericMethod &&
x.Name == "ResolveType")
.MakeGenericMethod(arg);
var delegate = Delegate.CreateDelegate(
typeof(Func<>).MakeGenericType(arg),
method);
Use that delegate:
return Activator.CreateInstance(_lazyType.MakeGenericType(arg), delegate);
This app outputs "True" and "0". I.e. ResolveTest(typeof(Lazy<int>)) returns a Lazy<int> object, constructed like you wanted.
using System;
using System.Linq.Expressions;
namespace TestApp
{
public class Class1
{
public static void Main()
{
object lazyInt = ResolveTest(typeof(Lazy<int>));
Console.WriteLine(lazyInt.GetType() == typeof(Lazy<int>));
Console.WriteLine(((Lazy<int>)lazyInt).Value);
}
static readonly Type _lazyType = typeof(Lazy<>);
static Object ResolveTest(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
{
var arg = type.GetGenericArguments()[0];
var lazyArgType = _lazyType.MakeGenericType(arg);
var funcArgType = typeof(Func<>).MakeGenericType(arg);
var funcCtor = lazyArgType.GetConstructor(new[] { funcArgType });
Expression<Func<object>> f = () => ResolveTest(arg);
var func = typeof(Class1).GetMethod("BuildCastedThing").MakeGenericMethod(arg).Invoke(null, new[] { f });
var arguments = new object[] { func };
var retVal = funcCtor.Invoke(arguments);
return retVal;
}
else
return ResolveType(type);
}
public static object ResolveType(Type type)
{
return Activator.CreateInstance(type);
}
public static Func<T> BuildCastedThing<T>(Expression<Func<object>> f)
{
Expression<Func<T>> expr =
Expression.Lambda<Func<T>>(
Expression.Convert(
Expression.Invoke(f),
typeof(T)));
return expr.Compile();
}
}
}
This is a way to rewrite ResolveTest as a generic Resolve<T> (e.g. Resolve<int> returns Lazy<int>). This is a little different, since there's no equivalent to ResolveTest(typeof(int)), which returns an int.
static Lazy<T> Resolve<T>()
{
var arg = typeof(T);
return new Lazy<T>(() => (T)ResolveType(arg));
}
Or with a generic ResolveType<T>:
static Lazy<T> Resolve<T>()
{
return new Lazy<T>(() => ResolveType<T>());
}
public static T ResolveType<T>()
{
return Activator.CreateInstance<T>();
}
public static Object ResolveTest(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == _lazyType)
{
var arg = type.GetGenericArguments()[0];
Expression<Func<object>> expressionWithFuncOfTypeObject = () => ResolveType(arg);
UnaryExpression expressionThatEvaluatesToAnObjectOfTypeArg = Expression.Convert(expressionWithFuncOfTypeObject.Body, arg);
LambdaExpression expressionWithFuncOfTypeArg = Expression.Lambda(typeof(Func<>).MakeGenericType(arg), expressionThatEvaluatesToAnObjectOfTypeArg);
Delegate funcOfTypeArg = expressionWithFuncOfTypeArg.Compile(); // <-- At runtime this will be of type Func<T>
return Activator.CreateInstance(_lazyType.MakeGenericType(arg), funcOfTypeArg);
}
else
return ResolveType(type);
}
I have an object model MyObject with various properties. At one point, I have two instances of these MyObject: instance A and instance B. I'd like to copy and replace the properties in instance A with those of instance B if instance B has non-null values.
If I only had 1 class with 3 properties, no problem, I could easily hard code it (which is what I started doing). But I actually have 12 different object models with about 10 properties each.
What's good way to do this?
Update
Use AutoMapper instead if you need to invoke this method a lot. Automapper builds dynamic methods using Reflection.Emit and will be much faster than reflection.'
You could copy the values of the properties using reflection:
public void CopyValues<T>(T target, T source)
{
Type t = typeof(T);
var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
foreach (var prop in properties)
{
var value = prop.GetValue(source, null);
if (value != null)
prop.SetValue(target, value, null);
}
}
I've made it generic to ensure type safety. If you want to include private properties you should use an override of Type.GetProperties(), specifying binding flags.
I have tried what's described by Merge Two Objects into an Anonymous Type by Kyle Finley and it is working perfectly.
With the TypeMerger, the merging is as simple as
var obj1 = new {foo = "foo"};
var obj2 = new {bar = "bar"};
var mergedObject = TypeMerger.MergeTypes(obj1 , obj2 );
That's it! You've got the merged object. Apart from that, there is a provision to ignore specific properties too. You can use the same thing for MVC3 as well.
You can do it using reflection, but as someone stated, it'll have a performance penalty.
Since you're working with an expected class design, you can achieve the same goal by using an extension method like so:
public static class MyClassExtensions
{
public static void Merge(this MyClass instanceA, MyClass instanceB)
{
if(instanceA != null && instanceB != null)
{
if(instanceB.Prop1 != null)
{
instanceA.Prop1 = instanceB.Prop1;
}
if(instanceB.PropN != null)
{
instanceA.PropN = instanceB.PropN;
}
}
}
And later, somewhere in your code:
someInstanceOfMyClass.Merge(someOtherInstanceOfMyClass);
At the end of the day you've centralized this operation in an extension method and if you add or remove a property of your class, you only need to modify extension method's implementation and you'll get everything done.
you can use this package:XASoft
use foo.Merger(bar) method
to combine 2 object to a dynamic object
just like this
var objA = new { a = 1, b = 2, c = 3 };
var newObj = objA.Merger(new { a = "Hey", d = 4, e = 5 });
newObj.e = "There";
newObj.f = 6;
Console.WriteLine(JsonConvert.SerializeObject(newObj));
even if object list works fine too!
private class TestClass
{
public int X { get; set; }
public int Y { get; set; }
}
static void Main(string[] args)
{
var list = new List<TestClass> {
new TestClass{ X=1,Y=2},
new TestClass{ X=3,Y=4}
};
Console.WriteLine(JsonConvert.SerializeObject(list.ListMerger(i => new
{ X = "null value", Z = i.X == 1 ? 0 : 1 })));
}
oops,looks like javascript language...
btw,source code click here
I've created a method similar to some answers above, but it returns a new object merged between 2 objects, and will only merge the second objects value if the first objects value is null.
public T MergeObjects<T>(T primary, T secondary)
{
T obj = (T)Activator.CreateInstance(typeof(T));
Type t = typeof(T);
var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
foreach (var prop in properties)
{
var value = prop.GetValue(primary, null);
if (value != null)
prop.SetValue(obj, value, null);
else
{
value = prop.GetValue(secondary, null);
if (value != null)
prop.SetValue(obj, value, null);
}
}
return obj;
}
I've written my own class for this purpose: ObjectMerger.
Basically it uses reflections (and may be slow because of that). It also contains more features e.g. parsing objects for cyclic references and merge them too. My ObjectMerger also contains mechanism to handle more complex classes like Delegate or MemberInfo. Those will be copied completely, other objects in the class are recursively merged.
The Syntax is like:
var initialInstance = new MyObjectBase(); // Initialize first object
var properInstance = new MyObjectWithAlgorithms(); // Initialize second object
var result = ObjectMerger.MergeObjects(properInstance, initialInstance); // Merge Objects into type of "properInstance"
I'm sorry to say that it is NOT FOR USE AS IS, because some external libraries are missing in the repository at the moment due to limitations in my company, but they can easily be rewritten. I hope a can add them in future.
This is the same as #Bas Answer but for Merging 2 Object lists
public class Copycontents
{
public static void Work<T>(IList<T> targetList, IList<T> sourceList, Func<T, int> selector)
{
var matchingPrimaryKey = targetList.Select(x => selector(x)).ToList();
foreach (var thismatchingPrimaryKey in matchingPrimaryKey)
{
CopyValues<T>(targetList.Single(x => selector(x) == thismatchingPrimaryKey),
sourceList.Single(x => selector(x) == thismatchingPrimaryKey));
}
}
private static void CopyValues<T>(T target, T source)
{
Type t = typeof(T);
var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
foreach (var prop in properties)
{
var value = prop.GetValue(source, null);
if (value != null)
prop.SetValue(target, value, null);
}
}
}