简体   繁体   中英

C# Service Collection adds type as object rather than its actual type

I'm having a little issue with my code.

Basically, I'm trying to dynamically add classes that I've created from deserializing JSON into my ServiceCollection, so I can get them from any class that needs them. So far I've got the following code:

Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(AbstractConfiguration)) && !t.IsAbstract).ToList().ForEach(x => {
    if (!File.Exists(x.Name + ".json")) {
        object Configuration = Activator.CreateInstance(x);
        File.WriteAllText(x.Name + ".json", JsonSerializer.Serialize(Configuration, new JsonSerializerOptions() { WriteIndented = true }));
        ServiceCollection.AddSingleton(Configuration);
     } else {
        string json = File.ReadAllText(x.Name + ".json");
        object Configuration = JsonSerializer.Deserialize(json, x, new JsonSerializerOptions() { WriteIndented = true });
        ServiceCollection.AddSingleton(Configuration);
    }
});

We load up our JSON into a class (which works), add it into our collection (which works), however; when adding into our collection the type is an object, so when I try call it through Services.GetServices(typeof(BotConfiguration)).ToList().Count); , it returns 0. What gives?

Well, if we instead try run Services.GetServices(typeof(object)).ToList().ForEach(x => Console.WriteLine(x.ToString())); , we can actually see that this instantiation actually falls under the object type, even though when we run x.ToString(), it shows that it is an instance of BotConfiguration (outputs Dexter.Core.Configurations.BotConfiguration in my case).

How would we get our ServiceCollection to add this as its actual class rather than an object? It clearly knows what type it is...?

You code is calling generic version of method ( AddSingleton<TService>(IServiceCollection, TService) ) with generic type parameter being resolved at compile time to object , try calling one accepting Type parameter (see all overloads ) like this:

ServiceCollection.AddSingleton(x);

UPD

There is overload ( AddSingleton(IServiceCollection, Type, Object) ) accepting type and object instance:

ServiceCollection.AddSingleton(x, Configuration);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM