Warcaith
Warcaith
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Oh, I see!
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
So basically, the "Execute" method will have no options parameter, but the class itself will have the options in a property?
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
How would I then specify which sort of options the command will use?
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
However... now we are back at the casting problems:
c#

public LaunchOptions {}

public LaunchCommand : Command<LaunchOptions> {}

List<Command<object>> commands = new List<Command<object>>();
commands.Add(new LaunchCommand());
c#

public LaunchOptions {}

public LaunchCommand : Command<LaunchOptions> {}

List<Command<object>> commands = new List<Command<object>>();
commands.Add(new LaunchCommand());
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
All commands are of type "Command", this one:
C#
public abstract class Command<TOptions, TResponse> : ICommand<TOptions, TResponse>
{
public abstract IResult<TResponse?> Execute(TOptions options);

public IResult<TResponse?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command<TOptions> : ICommand<TOptions>
{
public abstract IResult<object?> Execute(TOptions options);

public IResult<object?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command : ICommand
{
public abstract IResult<object?> Execute(object options);
}
C#
public abstract class Command<TOptions, TResponse> : ICommand<TOptions, TResponse>
{
public abstract IResult<TResponse?> Execute(TOptions options);

public IResult<TResponse?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command<TOptions> : ICommand<TOptions>
{
public abstract IResult<object?> Execute(TOptions options);

public IResult<object?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command : ICommand
{
public abstract IResult<object?> Execute(object options);
}
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
I mean, I need a registry somewhere, of some sort.
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Any suggestion for making it better? I'm eager to learn 🙂
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Yeah, but I do need to register them somewhere.
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Would this make sense @eXtensible Style Sheets?
c#
private static IResult<object?> ExecuteCommand(string name, string[] args)
{
switch(name)
{
case "Deploy":
return (new DeployCommand())
.Execute(CreateOptions<DeployOptions>(args));
case "Launch":
return (new LaunchCommand())
.Execute(CreateOptions<LaunchOptions>(args));
default:
throw new ArgumentException();
}
}
}
c#
private static IResult<object?> ExecuteCommand(string name, string[] args)
{
switch(name)
{
case "Deploy":
return (new DeployCommand())
.Execute(CreateOptions<DeployOptions>(args));
case "Launch":
return (new LaunchCommand())
.Execute(CreateOptions<LaunchOptions>(args));
default:
throw new ArgumentException();
}
}
}
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Yeah, either way, there's something missing from my design that I need to figure out 😄
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Hmm, I see...
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
For example, this doesn't work:
c#
class MyCommand : Command<CommandOptions>
{
...
}

ICommand<object> command = (Command<object>)new MyCommand();
c#
class MyCommand : Command<CommandOptions>
{
...
}

ICommand<object> command = (Command<object>)new MyCommand();
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
They do have relation
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
I mean, I can't cast to that class either.. is it because it's an abstract class?
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
However, I do have this abstract class:
public abstract class Command<TOptions, TResponse> : ICommand<TOptions, TResponse>
{
public abstract IResult<TResponse?> Execute(TOptions options);

public IResult<TResponse?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command<TOptions> : ICommand<TOptions>
{
public abstract IResult<object?> Execute(TOptions options);

public IResult<object?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command : ICommand
{
public abstract IResult<object?> Execute(object options);
}
public abstract class Command<TOptions, TResponse> : ICommand<TOptions, TResponse>
{
public abstract IResult<TResponse?> Execute(TOptions options);

public IResult<TResponse?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command<TOptions> : ICommand<TOptions>
{
public abstract IResult<object?> Execute(TOptions options);

public IResult<object?> Execute(object options)
{
return Execute((TOptions)options);
}
}

public abstract class Command : ICommand
{
public abstract IResult<object?> Execute(object options);
}
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Yeah, right, seems a bit unsafe.. 😄
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Do you happen to know why it doesn't work to cast to the interface?
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Here's basically what I was trying to do:
1. Program.exe MyCommandName
2. MyCommandName - Goes into -> FactoryMethod(MyCommandName)
3. FactoryMethod(MyCommandName) - Returns -> Command, Options
1. Program.exe MyCommandName
2. MyCommandName - Goes into -> FactoryMethod(MyCommandName)
3. FactoryMethod(MyCommandName) - Returns -> Command, Options
But as you said, it won't work, since I can't return anything generic enough.
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
I'll see if there's something weird with my implementation
22 replies
CC#
Created by Warcaith on 4/15/2024 in #help
Issue with generic interfaces (covariance/contravariance)
Yeah, I actually did exactly that after my post, however, when I tried to cast it, it still gave me the exception.
22 replies