C#结构-从外部库访问类 [英] C# Structure - Access class from external library

查看:75
本文介绍了C#结构-从外部库访问类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对构建应用程序的正确方法有疑问。我是C#的初学者,因此如果缺少一些基础,请找借口。
我看了网上(不仅是stackoverflow),但没有找到任何答案,所以也许这仅仅是因为我做错了什么?

I have a question regarding correct way to structure my application. I am a beginner on C# so excuse if I have some basis missing. I looked on the web(not only stackoverflow) but didn't find any answer, so maybe this is just because I am doing something wrong?

我有一个项目,其中有几个类来定义我的对象。

I have a project in which there are several classes to define my objects.

Object1.class.cs

Object1.class.cs

Object2 .class.cs
...

Object2.class.cs ...

现在,对于某些功能,我创建了类库(.dll文件)以外部化代码。这个想法是,如果我需要进行更新,我只需要替换.dll文件,而不是全部代码即可。

Now for some functions, I created class libraries (.dll files) to externalise the code. The idea is if I need to make an update, I just need to replace .dll file, and not all the code.

但是当我想要将对象发送到该类,我认为是因为它们在解决方案外部?

But I meet a problem when I want to send objects to that class, I think because they are external to the solution?

所以我开始怀疑我是否正确执行了操作。

So I begin to wonder if I do it correctly.

1)是否可以将项目中的对象发送到单独创建的类中?

1) Is there a way to send an object from my project to a class I created separately?

2)这是个好主意吗?这样工作吗?我的意思是,我当然可以在我的项目中添加该库,但是那样的话,我将没有单独的.dll文件,而所有文件都存储在一个.exe文件中。

2) Is this a good idea to work this way? I mean, I could of course add the library in my project, but in that case I will not have separated .dll files, and all get in a single .exe file.

3)当我需要测试班级时,总是需要关闭我的项目,然后打开班级进行编辑,然后回到我的项目中进行测试。有没有更简单的方法?

3) When I need to test my class, I always need to close my project, and open the class to edit it, then come back to my project to test it. Is there an easier way to do it?

在此先感谢您的建议

编辑2。

Simone Cifani很好地回答了我的问题:

Simone Cifani perfercly answered to my question :

1)必须将类外部化

2)在我的外部库中添加对必要类的引用

2) Add references to necessary classes in my external libraries

3)在我的主项目中添加对每个类和外部库的引用

3) Add references to each class and external libraries in my main project

由于我不知道如何使用接口,所以我会没有它,但是我认为即使没有接口也可以。

As I don't know how to use Interfaces I will do without it, but I think all will be fine even without.

推荐答案

将部分代码外部化的一种非常常见的方法是使用接口。

A very common way to externalize part of the code is by using interfaces.

一个简单的示例是:

//CalculatorInterface.dll
namespace Calculator.Interface
{
    interface ICalculator
    {
        int DoCalculus(List<object> list);
    }
}

//CalculatorImplementation_1.dll
//  Add a reference to CalculatorInterface.dll
namespace Calculator.Implementation
{
    using Calculator.Interface;

    class CalculatorImplementation_1 : ICalculator
    {
        public int DoCalculus(List<object> list)
        {
            int result = 0;

            foreach (Object obj in list)
            {
                if (obj is int)
                {
                    result += (int)obj;
                }
            }

            return result;
        }
    }
}

//Calculator.dll
//  Add a reference to CalculatorInterface.dll
//  Add a reference to CalculatorImplementation_1.dll (or dynamically load it)
namespace Calculator
{
    using Calculator.Interface;
    using Calculator.Implementation;

    class MyCalculator
    {
        void CalculateSomething()
        {
            List<Object> list = new List<Object>();
            list.Add(1);
            list.Add(2);
            list.Add("SomeString");

            ICalculator calculator = new CalculatorImplementation_1();

            int result = calculator.DoCalculus(list);

            Console.Write(result);
        }
    }
}

这篇关于C#结构-从外部库访问类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆