简体   繁体   English

如何在不添加对类库的引用的情况下访问另一个类库中的视图模型和项目模型

[英]How to access view models and models of project in another class library without adding reference to the class library

I am working on a WPF project using MVVM pattern. 我正在使用MVVM模式进行WPF项目。 In solution, I have viewmodels,models,views and properties.As per requirment, I need to access the same classes(view models, models, properties) from another class library in same project. 在解决方案中,我具有视图模型,模型,视图和属性。根据要求,我需要从同一项目中的另一个类库访问相同的类(视图模型,模型,属性)。 I do not want to add the reference to the class library as it is an exe file and a heavy component which has got so many classes which i do not require. 我不想添加对类库的引用,因为它是一个exe文件,是一个繁重的组件,具有很多我不需要的类。 So, is there any solution for this. 那么,对此有什么解决方案吗? How can i access same classes(view model,model,propeties) in a solution from another class library in same project? 如何从同一项目中的另一个类库访问解决方案中的相同类(视图模型,模型,属性)?

Thanks & Regards 感谢和问候

You have two choices. 您有两种选择。 First one, is to refactor your exe in order to extract the reusable classes to a separate dll. 第一个是重构您的exe,以便将可重用的类提取到单独的dll中。 Then, just add a reference to this new dll. 然后,只需添加对该新dll的引用。

The other is to use reflection to access the members of the exe, which is the worst option, even worse than simply adding a reference to the big old exe. 另一种方法是使用反射来访问exe的成员,这是最糟糕的选择,甚至比简单地添加对大型旧exe的引用更糟糕。

I suggest breaking down your solution further in more projects, For example separate projects for Model, View and ViewModel or perhaps breaking down even further and then add reference only to the library you need. 我建议在更多项目中进一步分解您的解决方案,例如针对Model,View和ViewModel的单独项目,或者可能进一步分解,然后仅添加对所需库的引用。 That way you are not exposing everything. 这样,您就不会暴露所有内容。

The other option is reflection which may be cumbersome to use and make your code ugly. 另一个选择是反射,使用起来可能很麻烦,并使您的代码难看。

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

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