It's there a way to invoke a method and the return type is strong typed ?
There is an example of code
public static IQueryable<T> FilterVersion<T>(this Table<T> t, IVersionIndexFilter version)
where T : class
{
try
{
// Define the new type of my table
Type versionTableType = Type.GetType(typeof(T).AssemblyQualifiedName.Replace(typeof(T).FullName, string.Concat(typeof(T).FullName, VersionIndexTableExtensionName)));
// Get the method info generic with a custom helper
var getTableType = MethodInfoHelper.GetGenericMethod(typeof(DataContext), "GetTable", new Type[] { versionTableType }, new Type[] { }, typeof(Table<>).MakeGenericType(typeof(Table<>).GetGenericArguments()[0]), BindingFlags.Public | BindingFlags.Instance);
// Get the object with a invoke but invoke return object, i need he return a Table<vesrionTableType>
var joinTable = getTableType.Invoke(t.Context, null);
// Put my new object in anonymous class
var result = new { FromT = t, InnerT = joinTable };
// Write the type of the property and is {System.Object InnerT}
Console.Write(result.GetType().GetProperty("InnerT"));
}
I need my Console.Write(result.GetType().GetProperty("InnerT"));
return a Table<versionTableType>
It's there a way i cand do that ? Any suggestion ?
There is my GetGenericMethod
public static MethodInfo GetGenericMethod(Type t, string name, Type[] genericArgTypes, Type[] argTypes, Type returnType, BindingFlags flags)
{
if (genericArgTypes == null)
{
genericArgTypes = new Type[] { };
}
MethodInfo genericMethod = (from m in t.GetMethods(flags)
where m.Name == name
&& m.GetGenericArguments().Length == genericArgTypes.Length
&& m.GetParameters().Select(pi => pi.ParameterType.IsGenericType ? pi.ParameterType.GetGenericTypeDefinition() : pi.ParameterType).SequenceEqual(argTypes) &&
(returnType == null || (m.ReturnType.IsGenericType ? m.ReturnType.GetGenericTypeDefinition() : m.ReturnType) == returnType)
select m).FirstOrDefault();
if (genericMethod != null)
{
return genericMethod.MakeGenericMethod(genericArgTypes);
}
return null;
}
I get my method info generic correctly. The probleme is when i assign to another property the result of my invoke is an object. I need to strong type my result
EDIT : It's a good idea the static method CreateResult but not enough ... there is what i try to do after with my value
NewExpression exResult = Expression.New(result.GetType().GetConstructor(new Type[] { t.GetType(), typeof(Table<>).MakeGenericType(versionTableType) }), new List<Expression>() { outer, inner }, result.GetType().GetProperty("FromT"), result.GetType().GetProperty("InnerT"));
There is the error i got after try the technique
Argument type 'System.Data.Linq.Table`1[Nms.Media.Business.Manager.Data.Linq.ImageLibraryItemBinaryLocaleVersionIndex]' does not match the corresponding member type 'System.Object'
Thanks