简体   繁体   English

C# 服务集合将类型添加为对象而不是其实际类型

[英]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.基本上,我正在尝试将我通过反序列化 JSON 创建的类动态添加到我的 ServiceCollection 中,以便我可以从需要它们的任何类中获取它们。 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;我们将 JSON 加载到一个类中(有效),然后将其添加到我们的集合中(有效); when adding into our collection the type is an object, so when I try call it through Services.GetServices(typeof(BotConfiguration)).ToList().Count);当添加到我们的集合中时,类型是一个对象,所以当我尝试通过Services.GetServices(typeof(BotConfiguration)).ToList().Count);调用它时Services.GetServices(typeof(BotConfiguration)).ToList().Count); , it returns 0. What gives? ,它返回 0。什么给出了?

Well, if we instead try run Services.GetServices(typeof(object)).ToList().ForEach(x => Console.WriteLine(x.ToString()));好吧,如果我们改为尝试运行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). ,我们实际上可以看到这个实例化实际上属于对象类型,即使当我们运行 x.ToString() 时,它显示它是 BotConfiguration 的一个实例(在我的例子中输出 Dexter.Core.Configurations.BotConfiguration)。

How would we get our ServiceCollection to add this as its actual class rather than an object?我们如何让我们的 ServiceCollection 将它添加为它的实际类而不是一个对象? 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:您的代码正在调用通用版本的方法( AddSingleton<TService>(IServiceCollection, TService) ),在编译时将泛型类型参数解析为object ,尝试调用一个接受Type参数(查看所有重载),如下所示:

ServiceCollection.AddSingleton(x);

UPD UPD

There is overload ( AddSingleton(IServiceCollection, Type, Object) ) accepting type and object instance:有重载( AddSingleton(IServiceCollection, Type, Object) )接受类型和对象实例:

ServiceCollection.AddSingleton(x, Configuration);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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