score:3
Look at this:
s => s.Length;
How's the compiler suppose to know whether or not s
is a string
or s
is an array or some other type that has a Length
property? It can't, unless you give it some information:
(string s) => s.Length;
Oh, there we go. So now, try this:
myFirstObject.Extension((myOtherObject o) => o.Prop > 2 && o.Prop < 15);
That will work, because you've told the compiler what it should use for TIn
, and it can figure out what to use for TKey
based on the expression.
score:0
When you call a generic method in C# you can explicitly declare all of the generic type parameters or you can have them all inferred, but you cannot have some explicitly declared and some inferred.
So, if I had this method:
public void Foo<X, Y>(X x, Y y)
{
/* Do somethhing */
}
Then here's what works and what doesn't:
int a = 42;
string b = "Hello, World!";
// Legal
Foo(a, b);
Foo<int, string>(a, b);
//Illegal
Foo<int>(a, b);
The best you can do is move the first generic parameter up to the class level, but not it won't work as an extension method. Nevertheless you may like this approach.
public static class Class<TSource>
{
public static bool Method<TIn, TKey>(
TSource p_Value,
Expression<Func<TIn, TKey>> p_OutExpression)
{
return true;
}
}
Now you can call it like this:
Expression<Func<long, decimal>> f =
l => (decimal)l;
var result = Class<int>.Method(a, f);
But as I say, it won't work as an extension method now.
score:0
I found that another solution would be to create another method that takes in argument a type.
For instance:
Void Extension(Type p_Type, [THE TYPE] p_Params)
{
MethodInfo realExtensionMethod = typeof([CLASS CONTAINING THE METHOD]).GetMethod("RealExtension");
realExtensionMethod = realExtensionMethod.MakeGenericMethod(p_Type);
realExtensionMethod.Invoke(null, new object[] {p_Type, p_Params });
}
Void RealExtension<TYPE>(params)
{
}
Then at usage time:
Type objectType = typeof(myOtherObject);
myFirstObject.Extension(objectType, other => other.Prop );
Source: stackoverflow.com
Related Query
- Extension method with generic Func parameter of other type
- Generic extension method with custom return type
- Calling generic method with a type argument known only at execution time
- How do I build Expression Call for Any Method with generic parameter
- Call Generic Method with an anonymous Type (C#)
- c# constraints ignored in extension with generic type
- Extension method with optional generic argument
- Dynamic Linq on method called with generic type via reflection
- Linq to SQL: delete record with generic type method
- Convert Generic Method Parameter to Specific Type
- Linq in conjunction with custom generic extension method build error - An expression tree may not contain an assignment operator?
- The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to ''
- C# - Generic method with linq parameter
- Using method parameter as generic type argument
- Enumerable.Where with a collection as parameter instead of extension method
- 'No generic method > 'OrderByDescending' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments
- Invoking Generic Method with the parameter of IEnumerable<T>
- No generic method 'Where' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments
- Using Generic with Func as a parameter
- Expression Func with two input parameters for generic method
- Generic method with dynamically selected parameter in predicate
- how to make asynchronous static method with same generic array param and same generic array return type
- Cannot call method of type A with parameter type A?
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq error generic parameter or the query must use a nullable type
- When to use an extension method with lambda over LINQtoObjects to filter a collection?
- Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method
- Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
- "CLR detected an Invalid Program" when using Enumerable.ToDictionary with an extension method
- Writing an extension method to help with querying many-to-many relationships
More Query from same tag
- Asp.Net MVC LINQ query to check if agreement already exists between 2 dates
- Getting distinct rows in LINQ
- How to get XDocument element values
- Is there a way to search in Dictionary<int[], List<int> by values inside values faster than this code already does?
- C#: Count occurrences of a string in a list which is in another list using LINQ?
- Groupby and Compare the results in Linq
- How to write nested query in Linq
- I have some problems with LINQ expression, OrderBy(), Skip(), Take() works incorrect
- Filtering using AsQueryable in Mongodb C# - ExpandoObject
- How to delete every 2nd character in a string?
- Linq To XML problems and databinding
- How to Filter Inner Collection using LINQ C#
- DbContext times out on remote server only
- Set selected values in asp ListBox with one line
- Filter a list based on multiple criteria and multiple values (unknown number of values)
- LINQ: How to refresh a GridView bound to LinqDataSource?
- Get maxdate less than the current date
- Error using EF6 DbFunctions.TruncateTime comparing DateTime
- Remove Duplicates from dropdown using Linq
- Problems with looping over XML in C# MVC
- How to force Linq to join tables
- How to write this SQL average query in linq
- Linq Expressions and "Not Contains" query
- How to use nullable built-in variables in LINQ?
- Is there a better way to achieve this with Linq or a better code
- Retrieving entities from a one to many relationship (Odata)
- Assigning Querystring variable with Linq
- Conditional pairing of two streams - Combine If in Reactive Extensions
- c# LINQ-to-XML update (save) list of elements in file after ordering
- SQL to Linq Lambda