C
C#•3y ago
Thinker

Type cannot be found (CS7069) [Answered]

I'm getting an error Reference to type 'ITypeRegistrar' claims it is defined in 'Spectre.Console', but it could not be found, despite the fact that I do have a reference to the Spectre.Console package. I have never encountered this error before and I have no idea how to resolve it.
10 Replies
Thinker
ThinkerOP•3y ago
My package references are
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Registrars.SimpleInjector" Version="0.3.0" />
<PackageReference Include="SimpleInjector" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Registrars.SimpleInjector" Version="0.3.0" />
<PackageReference Include="SimpleInjector" Version="5.0.0" />
</ItemGroup>
The error only started occurring after I added the last two packages in the list, so I have no idea if they affected something. Okay for more context
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;
using Reline.Cli;

Container container = new();
SimpleInjectorRegistrar registrar = new(container);
CommandApp<CompileCommand> app = new(registrar); // CS7069 + CS1503
return app.Run(args);
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;
using Reline.Cli;

Container container = new();
SimpleInjectorRegistrar registrar = new(container);
CommandApp<CompileCommand> app = new(registrar); // CS7069 + CS1503
return app.Run(args);
It also claims that it cannot convert from SimpleInjectorRegistrar to ITypeRegistrar (which is required by the CommandApp constructor)... but SimpleInjectorRegistrar implements ITypeRegistrar. This makes no sense
Thinker
ThinkerOP•3y ago
You mean PrivateAssets="all"?
LPeter1997
LPeter1997•3y ago
Yes and they are even different versions 0.44
Thinker
ThinkerOP•3y ago
huh wait what does this mean, the version of Spectre.Console is different from that used by the package? Idk how that would matter
LPeter1997
LPeter1997•3y ago
Well my main concern is private assets
Thinker
ThinkerOP•3y ago
What does private assets actually do? I've seen it before (especially for source generators) but I don't know what it actually does Ah, it hides that library as a dependency from consuming libs. Explains why I had to manually install SimpleInjector Sooo... is there any way to solve this?
LPeter1997
LPeter1997•3y ago
I've pasted in the source code for SimpleInjectorRegistrar, removed the ref to Spectre.Console.Registrars.SimpleInjector and it works 😄 The whole package is a single file anyway
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;

Container container = new();
SimpleInjectorRegistrar registrar = new(container);
CommandApp<CompileCommand> app = new(registrar); // CS7069 + CS1503
return app.Run(args);

public class SimpleInjectorRegistrar : ITypeRegistrar
{
private readonly Container container;
private readonly Lifestyle lifestyle;

public SimpleInjectorRegistrar(Container container, Lifestyle? lifestyle = null)
{
this.container = container ?? throw new ArgumentNullException(nameof(container));
this.lifestyle = lifestyle ?? Lifestyle.Singleton;
}

public void Register(Type service, Type implementation) =>
container.Register(service, implementation, lifestyle);

public void RegisterInstance(Type service, object implementation) =>
container.RegisterInstance(service, implementation);

public void RegisterLazy(Type service, Func<object> factory) =>
container.Register(service, factory, lifestyle);

public ITypeResolver Build()
{
container.Verify();
return new SimpleInjectorTypeResolver(container);
}

private class SimpleInjectorTypeResolver : ITypeResolver
{
private readonly Container container;

public SimpleInjectorTypeResolver(Container container)
{
this.container = container;
}

public object? Resolve(Type? type)
{
if (type is null) return null;
object? implementation = null;
try
{
implementation = container.GetInstance(type);
}
catch (ActivationException)
{
}
return implementation;
}
}
}
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;

Container container = new();
SimpleInjectorRegistrar registrar = new(container);
CommandApp<CompileCommand> app = new(registrar); // CS7069 + CS1503
return app.Run(args);

public class SimpleInjectorRegistrar : ITypeRegistrar
{
private readonly Container container;
private readonly Lifestyle lifestyle;

public SimpleInjectorRegistrar(Container container, Lifestyle? lifestyle = null)
{
this.container = container ?? throw new ArgumentNullException(nameof(container));
this.lifestyle = lifestyle ?? Lifestyle.Singleton;
}

public void Register(Type service, Type implementation) =>
container.Register(service, implementation, lifestyle);

public void RegisterInstance(Type service, object implementation) =>
container.RegisterInstance(service, implementation);

public void RegisterLazy(Type service, Func<object> factory) =>
container.Register(service, factory, lifestyle);

public ITypeResolver Build()
{
container.Verify();
return new SimpleInjectorTypeResolver(container);
}

private class SimpleInjectorTypeResolver : ITypeResolver
{
private readonly Container container;

public SimpleInjectorTypeResolver(Container container)
{
this.container = container;
}

public object? Resolve(Type? type)
{
if (type is null) return null;
object? implementation = null;
try
{
implementation = container.GetInstance(type);
}
catch (ActivationException)
{
}
return implementation;
}
}
}
Like... this class is the whole package, IDEK why it has to be a separate nuget thing It didn't even utilize nullables 😅
Thinker
ThinkerOP•3y ago
Yeah, the entire point was kinda not having to paste it in the project as it's just a redundant wrapper class, but yeah that works I suppose.
Accord
Accord•3y ago
✅ This post has been marked as answered!

Did you find this page helpful?