Verdur
Verdur
CC#
Created by Verdur on 5/18/2024 in #help
Check whether list of generics contains a type, that implements interface
Hello, I need to perform sophisticated generic type constraint. I have following class to store multiple generics in my class, they are added to class after each method call:
interface ICons<TLeft, TRight> {}
interface ICons<TLeft, TRight> {}
And after calling few methods I get following type:
class Builder<T> { ... }

Builder<ICons<IWithBar, ICons<IWithFoo, IUninitialized>> builder = new Builder().Foo(...).Bar(...);
class Builder<T> { ... }

Builder<ICons<IWithBar, ICons<IWithFoo, IUninitialized>> builder = new Builder().Foo(...).Bar(...);
I have a function that accepts Builder<IConst<TLeft, TRight>> and I need generic constraint that will accept only those Builder's, which have both IWithFoo and IWithBar somewhere inside their IConst<TLeft, TRight>. Something like this:
void Build<TLeft, TRight>(Builder<ICons<TLeft, TRight>> builder)
where "TLeft or TRight contain somewhere inside them IWithFoo and IWithBar"
{ ... }
void Build<TLeft, TRight>(Builder<ICons<TLeft, TRight>> builder)
where "TLeft or TRight contain somewhere inside them IWithFoo and IWithBar"
{ ... }
I tried another approach where I specified that Build<T> accepts T where T : IWithBar, IWithFoo, but that would require ICons<TLeft, TRight> to implement TLeft and TRight, which I was answered is not possible (here: https://discord.com/channels/143867839282020352/1241306845940027472)
1 replies
CC#
Created by Verdur on 5/18/2024 in #help
Interface implementing generic parameters
Hello, is there way to achieve something like this?
interface IFoo<T> : T { }
interface IFoo<T> : T { }
I want to specify that IFoo<T> should also implement T
4 replies
CC#
Created by Verdur on 1/4/2024 in #help
✅ Complicated error with C# SignalR client
Hi, I'm trying to receive base class in signalr using connection.on function. My base class has inherits and they all may be sent to the client. Problem is in connection.on<T> method. It converts whatever JSON serialized data comes to it straight to the T type, ignoring T's dervied types. I have tested it by wrapping my Foo class in wrapper class Bar and passing it to the method:
c#
connection.On<Bar>(...)
c#
connection.On<Bar>(...)
This way, if I store Baz: Foo type in Bar and pass it to client, it will recieve Bar with foo field having Baz type. Is there a way to make connection.on<T> convert incoming data not just to T, but to it's derivative too without using wrapper classes?
22 replies