Nested generics <T1<T2>> - c#

Why it is impossible to write something like this:
T1<T2> Method<T1, T2>(T1<T2> genericValue) { ... }
For example, I want to write an extension method which accepts a generic object, containing a generic colletion:
Container<CT<T>> ExtensionMethod<CT, T>(Container<CT<T>> value) { ... }
where CT can be Array, List or any other collection type and T is any type. But Compiler says "Type parameter T1 does not have type parameters".
Is there any workaround for that?

This is not possible. Your generic has to compile for any T. So if you make any assumption about T other than it being object you have to add constraints with the where keyword.
This constraints system is not very complex. You cannot add a constraint asking for T to be a class with exactly one generic type parameter. So what you want to do is impossible with the current toolset.
You would need to ask Microsoft why they did not implement it, but it appears they saw not enough business value.
Maybe you could ask a question how to implement something, without breaking it down to generics. It seems you may have an XY Problem.

Related

Using nameof for a recursive generic type's property

I have the following class
public abstract class Result<TResult, TData> where TResult : Result<TResult, TData>
{
public virtual TData Data { get; private set; }
}
How can I use nameof on the Data property?
It tried some options but got compilation errors
nameof(Result<object, object>.Data)
Error CS0311
The type 'object' cannot be used as type parameter 'TResult' in the generic type or method
'Result<TResult, TData>'. There is no implicit reference conversion from 'object' to
'Result<object, object>'
UPDATE
To better specify:
I did not write the class in the first snippet, but I have to use it as it is.
I need to use reflection to get a PropertyInfo instance for the Data property. I can do it by calling GetProperty("Data"), but I prefer to avoid strings in case the name property is renamed with the Visual Studio Refactoring (for example from Data to Content).
I cannot use nameof(Data) and I am in a different context that doesn't know about the Data property in that class.
As such, I would call GetProperty(nameof({whatever}.Data)). Of course, if not possible I would use the string. But if there is a way, I would like to know it.
Unfortunately, the nameof operator does not work with unbound generic types. E.g. you'd like to be able to write nameof(Result<,>.Data), just like you can today write something like typeof(Result<,>). So you would need to specify type parameters when trying to get the nameof(Result<TResult, TData>.Data) value.
Which you've tried, but you provided object as the type parameter for both parameters, even though your generic type constrains TResult as being derived from Result<TResult, TData>. The type object doesn't meet the constraint, so that can't possibly work. Hence the compiler error.
Obviously, if you can provide any type that does meet the constraint, that would solve the compiler error and allow you to use the nameof operator. There's not enough information in your question to know whether that's an option in your specific scenario.
I agree with this comment that you would probably be better off asking a different question, one which takes a step back and explains how you arrived at feeling you needed this syntax in the first place. It's not clear what the broader goal you're trying to accomplish is, where you don't have known type parameters to use for this expression. Typically, code outside of the generic type that wants to make use of the generic type, would actually know the type parameters it intends to use with the generic type.
Note that in the context of the generic type itself, you can refer to the property without knowing the exact type parameters, since the property identifier does not need qualification. E.g. nameof(Data) would work, for any code that's actually in the generic class Result<TResult, TData>. Whether that helps in your specific scenario is unclear from the information provided so far.

nameof() operator for property of generic class [duplicate]

I am trying to get the name of a method on a generic interface. I would expect this to work as the type part would be a valid typeof:
//This does not compile
nameof(IGenericInterface<>.Method)
//This would compile
typeof(IGenericInterface<>)
I think this should be valid c#-6.0 or am I missing something or is there a better way to do this. I don't want to use a string for the Method name as if the method is renamed code would break without any build-time errors.
This is expected. According to the documentation, your expression is disallowed, because it refers to an unbound generic type:
Because the argument needs to be an expression syntactically, there are many things disallowed that are not useful to list. The following are worth mentioning that produce errors: predefined types (for example, int or void), nullable types (Point?), array types (Customer[,]), pointer types (Buffer*), qualified alias (A::B), and unbound generic types (Dictionary<,>), preprocessing symbols (DEBUG), and labels (loop:).
You can work around this limitation by supplying a generic parameter:
nameof(IGenericInterface<object>.Method)
Note: I think Microsoft should tweak nameof feature to allow references to methods of unbound generic types.
Just use a sample type in order to compile.
string name = nameof(IGenericInterface<int>.Method) // will be Method
The solutions presented with "sample types" will work, but sooner or later you will need to get the nameof a generic type which has type constraints, so nameof(MyGenericType<object>) won't work, because object does not abide by the constraints.
If you find yourself in this situation, it might seem that you have to declare a dummy little class real quick which abides by the constraints so that you can get its nameof, but having to do something as hacky as that is a clear indication that you are down the wrong rabbit hole.
Here is a better solution:
typeof(MyGenericType<>).Name
Interestingly enough, C# allows us to use <> in typeof but not in nameof. Go figure.

Regarding generic Where T

void LoadParameters<T, TValue>(TValue strategy)
where T : Parameters
Saw a code like this. Will the program still work correctly without the where statement?
If it compiles without the where statement, it will work correctly. However, chances are the coder did not simply put it there without reason. If you remove it, it will likely not compile and hence not work.
That where clause is called a "generic type constraint". Usually T can be any type and you must write your generic code with that in mind. By constraining T to be, inherit or implement a specific type, you gain the ability to refer to the members of that type in your generic code, because the compiler is assured that any object used will be that type. You can also use class, struct or new as generic type constraints, which enforce T being a reference type, a value type or having a parameterless constructor respectively.

Inferring generic types from one method to another

I'm looking for a way to do this:
var instanceOfMyClass = new MyClass();
instanceOfMyClass.AMethod<AType>(x => x.AnotherMethod(y => y.PropertyOfATypeClass));
I can do this but i need to explicitly specify the type for another class, like so:
instanceOfMyClass.AMethod<AType>(x => x.AnotherMethod<AType>(y => y.PropertyOfATypeClass));
Is there a way to do this without needing to rewrite the AType?
The AnotherMethod signature, by now, is:
AnotherMethod<T>(Expression<Func<T, object>>)
where that
<T>
would be the same AType from "AMethod"
The "AnotherMethod" is defined in the same class as "AMethod".
I need the AMethod's signature.
It appears that you have encountered one of the limitations of the C# type inference engine.
The crux of issue is that, in the general case of method group type inference, in order to do type inference on the parameters of a method that takes generic delegates, the compiler needs to know the method signature (to determine which types of delegates there are overloads for); but to determine the method signature, it needs to know the types of your delegate parameters. Thus, its possible for the compiler to get into a cyclical process. To avoid this, the compiler won't even try to do the inference.
For a much more accurate, detailed, and well-written explanation, see this blog post.
And I think the short answer is, no, there's no way to avoid re-specifying the generic type.

Reasons to specify generic types in LINQ extension methods

Just out of curiosity:
Many LINQ extension methods exist as both generic and non-generic variants, for example Any and Any<>, Where and Where<> etc. Writing my queries I usually use the non-generic variants and it works fine.
What would be the cases when one has to use generic methods?
--- edit ---
P.S.: I am aware of the fact that internally only generic methods are called and the compiler tries to resolve the content of the generic brackets <> during compilation.
My question is rather what are the cases then one has to provide the type explicitly and not to rely on the compiler's intuition?
Always. The C# compiler is smart enough to infer what the type of the method is based on the parameters. This is important when the type is anonymous, and thus has no name.
obj.SomeMethod(123); //these calls are the same
obj.SomeMethod<int>(123);
obj.SomeMethod(new { foo = 123 }); //what type would I write here?!
Edit: To be clear, you are always calling the generic method. It just looks like a non-generic method, since the compiler and Intellisense are smart.
Edit: To your updated question, you would want to be specific if you want to use a type that is not the type of the object you are passing. There are two such cases:
If the parameter implements an interface, and you want to operate on that interface, not the concrete type, then you should specify the interface:
obj.DoSomething<IEnumerable<Foo>>( new List<Foo>() );
If the parameter is implicitly convertible to another type, and you want to use the second type, then you should specify it:
obj.DoSomethingElse<long> ( 123 ); //123 is actually an int, but convertible to long
On the other hand, if you need a cast to do the conversion (or you insert one anyway), then you don't need to specify:
obj.DoYetAnotherThing( (Transformed)new MyThing() ); // calls DoYetAnotherThing<Transformed>
One example I ran into today:
ObjectSet<User> users = context.Users;
var usersThatMatch = criteria.Aggregate(users, (u, c) => u.Where(c));
The above code won't work because the .Where method doesn't return an ObjectSet<User>. You could get around this one of two ways. I could call .AsQueryable() on users, to make sure it's strongly typed as an IQueryable, or I could pass specific type arguments into the Aggregate method:
criteria.Aggregate<Func<User, bool>, IEnumerable<User>>(
PersonSet, (u, c) => u.Where(c));
Another couple of more common examples are the Cast and OfType methods, which have no way to infer what type you want, and in many cases are being called on a non-generic collection in the first place.
In general, the folks that designed the LINQ methods went out of their way to avoid the need to use explicit types in these generic methods, and for the most part you don't need to. I'd say it's best to know it's an option, but avoid doing it unless you find it necessary.

Categories

Resources