如何创建通常使用通用方法生成的 class 的实例?

How can I create an Instance of a class that is normally generated with a generic method?

我正在实现一个库,我必须在其中进行一些转换才能获得 class 的实例,从 .dll 的文档中我们可以找到一个工作代码示例

   public void Example()
    {
        DeviceItem deviceItem = ...;
        GsdDeviceItem gsdDeviceItem =((IEngineeringServiceProvider)deviceItem).GetService<GsdDeviceItem>();             

        var attributeNames = new[] { "TypeName", "Author", "Comment" };

        foreach (var attributeName in attributeNames)
        {
            object attributeValue =
                ((IEngineeringObject)gsdDeviceItem).GetAttribute(attributeName);
        }
    }    
        

我想实现一个看起来像这样的方法:

public object GetAttributeFromService(DeviceItem item, Type serviceType, string attributeName)
    {

        object a = Activator.CreateInstance(((IEngineeringServiceProvider)item).GetService<serviceType>());

        return ((IEngineeringObject)a).GetAttribute(attributeName);
        
    }

我收到错误 CS0118 'serviceType' 是一个变量,但像类型一样使用,如果有人能帮助我就太好了

如果IEngineeringServiceProvider继承自IServiceProvider,使用:

var service = Activator.CreateInstance(((IEngineeringServiceProvider)item).GetService(serviceType));

就这些了