【问题标题】:Compiling class in DLL and referencing it in another project in C#在 DLL 中编译类并在 C# 中的另一个项目中引用它
【发布时间】:2013-10-23 13:18:40
【问题描述】:

希望我问的正确。

我有这门课:

class ListUtilities
{
    private List<string> _datalist;

    public ListUtilities(List<string> datalist)
    {
        _datalist = datalist;
    }

    //Method to calculate age from a date
    private int getAge(string bdaystr)
    {
        DateTime today = DateTime.Today;

        DateTime bday = new DateTime(Convert.ToInt32(bdaystr.Substring(0, 4)), Convert.ToInt32(bdaystr.Substring(4, 2)), Convert.ToInt32(bdaystr.Substring(6, 2)));

        int age = today.Year - bday.Year;
        if (bday > today.AddYears(-age)) age--;

        return age;
    }

    //Method to print the data List
    public void printDataList()
    {
        for (int i = 0; i < _datalist.Count; i++)
        {
            Console.WriteLine(_datalist.ElementAt(i));
        }
    }

    //Method to calculate and print the ages
    public void printAges()
    {
        List<int> ages = new List<int>();

        for (int i = 0; i < _datalist.Count; i++)
        {
            string s = _datalist.ElementAt(i);
            string[] data = s.Split(',');

            ages.Add(getAge(data[3]));
        }

        Console.WriteLine("Ages:");

        for (int i = 0; i < ages.Count; i++)
        {
            Console.WriteLine(ages.ElementAt(i));
        }
    }

    //Method to search by surname
    public string searchBySurname(string surname)
    {
        string res = "";
        res = _datalist.Find(delegate(String str)
        {
            string[] data = str.Split(',');
            string sname = data[1];

            if (sname == surname)
                return true;
            else
                return false;
        });

        return res;
    }

    //Method to search by phone number
    public string searchByPhoneNumber(string phone)
    {
        string res = "";
        res = _datalist.Find(delegate(String str)
        {
            string[] data = str.Split(',');
            string phn = data[4];

            if (phn == phone)
                return true;
            else
                return false;
        });

        return res;
    }

    //Method to search by age
    public string searchByAge(int age)
    {
        string res = "";
        res = _datalist.Find(delegate(String str)
        {
            string[] data = str.Split(',');
            int age2 = Convert.ToInt32(getAge(data[3]));

            if (age2 == age)
                return true;
            else
                return false;
        });

        return res;
    }

    //Method to sort by surname
    public int sortBySurname(string x, string y)
    {
        string[] data_x = x.Split(',');
        string sname_x = data_x[1];

        string[] data_y = y.Split(',');
        string sname_y = data_y[1];

        return String.Compare(sname_x, sname_y);

    }

    //Method to sort by phone number
    public int sortByPhoneNumber(string x, string y)
    {
        string[] data_x = x.Split(',');
        int phn_x = Convert.ToInt32(data_x[4]);

        string[] data_y = y.Split(',');
        int phn_y = Convert.ToInt32(data_y[4]);

        return phn_x.CompareTo(phn_y);
    }

    //Method to sort by age
    public int sortByAge(string x, string y)
    {
        string[] data_x = x.Split(',');
        int age_x = Convert.ToInt32(data_x[3]);

        string[] data_y = y.Split(',');
        int age_y = Convert.ToInt32(data_y[3]);

        return age_y.CompareTo(age_x);
    }
}

我想将它编译成一个 .DLL 文件。我曾尝试通过这样的控制台进行操作:

csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs

并将该类放入类库项目并构建它,我在这两种情况下都获得了 DLL 文件,但是当我想使用该 DLL 时问题就来了。

我创建了一个新项目,我确实引用了 DLL 文件,但是当我想使用它时,问题就来了:

而且,DLL 中似乎没有任何内容:

最好的问候

【问题讨论】:

  • 如果你有 Visual Studio 为什么要通过命令行编译?
  • 为什么不使用 msbuild。 msbuild.exe sample.csproj /p:Configuration=Release;PackageAsSingleFile=False;outdir=destPath;

标签: c# visual-studio-2012 dll import


【解决方案1】:
class ListUtilities

默认类是internal,只能从同一个程序集中看到 - 让你的类public,它应该可以正常工作:

public class ListUtilities

【讨论】:

  • 是的,没注意到。非常感谢! ;)
【解决方案2】:

您的课程需要公开。默认情况下,类是内部的,这意味着它们只能在 DDLL 内部看到。

【讨论】:

    【解决方案3】:

    你需要公开你的课程

    public class ListUtilities
    

    因为类是默认的internal

    附带说明:

    您可以尝试使用 Visual Studio 而不是命令行,这会让您更轻松。

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2011-01-19
      相关资源
      最近更新 更多