使用反射和结构图

发布于 2024-12-26 11:25:33 字数 731 浏览 2 评论 0原文

我正在使用结构图自动将 DataContext 注入到我的存储库构造函数中。我得到了一个名称(例如“Project1”),我需要动态创建项目存储库的实例。

我使用标准命名约定,所以我知道它是“Project1DataContext”。我已设法使用反射创建 Project1DataContext 的实例,但它是对象类型。问题是我需要将 Project1DataContext 对象传递到我的存储库中以创建它的实例。我如何使用反射来做到这一点?是否可以通过某种方式来投射该物体?

Assembly myDataContextAssembly = typeof(SomeTypeInTheAssembly).Assembly;
Type dataContextType = myDataContextAssembly.GetType(ProjectName + "DataContext");
object dataContext = Activator.CreateInstance(dataContextType);    
// I need to cast the data context here
IRepository<Project1DataContext> = new Repository<Project1DataContext>(dataContext)

同时,我将使用 if 语句,但如果我有 100 多个项目,这不是一个可行的解决方案。我需要使用反射来完成此操作,理想让结构图确定类型并为我注入它们。

I'm using structure map to automatically inject a DataContext into my Repository constructor. I'm given a name (e.g. "Project1") and I need to dynamically create an instance of the Repository for project.

I'm using a standard naming convention, so I know it's "Project1DataContext". I've managed to create an instance of my Project1DataContext using reflection, but it's an object type. The problem is I need to pass a Project1DataContext object into my Repository to create an instance of it. How can I do this using reflection? Would it be possible to cast the object through some means?

Assembly myDataContextAssembly = typeof(SomeTypeInTheAssembly).Assembly;
Type dataContextType = myDataContextAssembly.GetType(ProjectName + "DataContext");
object dataContext = Activator.CreateInstance(dataContextType);    
// I need to cast the data context here
IRepository<Project1DataContext> = new Repository<Project1DataContext>(dataContext)

In the mean time, I will use an if statement, but this is not a viable solution if I have 100+ projects. I need to do this using reflection and ideally having structure map determine the types and injecting them for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2025-01-02 11:25:33

根据我从所提供的信息中了解到的情况,您希望在将该类型传递给 IRepository 和 Repository 泛型类之前将 dataContext 对象转换为其真实类型。这意味着您想让它们具体化。您可以在运行时使它们具体化,但不能通过将 Type 对象作为泛型参数传递来实现。此外,您不能在这里依赖泛型类型推断,因为这仅在编译时完成。

我假设你的方法返回一个 IRepository 或 Respository (没有通用参数)。

这是您需要做的:为 Repository<> 创建一个具体类型使用 dataContextType,然后使用该具体类型创建一个 Repository 对象,将其转换为 Repository,然后返回它。

        Assembly myDataContextAssembly = typeof(SomeTypeInTheAssembly).Assembly;
        Type dataContextType = myDataContextAssembly.GetType(ProjectName + "DataContext");
        Type concreteRepositoryType = typeof(Generic<>).MakeGenericType(dataContextType);
        Repository repository = (Repository)System.Activator.CreateInstance(concreteRepositoryType);
        return repository;

Given what I understand from the information provided, you want to cast the dataContext object to its real type before you pass that type to the IRepository and Repository generic classes. Which means that you want to make them concrete. You can make them concrete during runtime but not via passing a Type object as a generic argument. Also, you can't rely on generic type inference here because that is only done at compile time.

I will suppose that your method returns an IRepository or Respository (without generic parameters).

Here is what you need to do: Create a concrete type for Repository<> using dataContextType and then use that concrete type to create a Repository object that you cast to Repository and then return it.

        Assembly myDataContextAssembly = typeof(SomeTypeInTheAssembly).Assembly;
        Type dataContextType = myDataContextAssembly.GetType(ProjectName + "DataContext");
        Type concreteRepositoryType = typeof(Generic<>).MakeGenericType(dataContextType);
        Repository repository = (Repository)System.Activator.CreateInstance(concreteRepositoryType);
        return repository;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文