在DLL中编译类并在C#中的另一个项目中引用它

时间:2013-10-23 13:18:41

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

希望我能正确地问这个问题。

我有这堂课:

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文件,但是当我想使用它时,这就是问题所在:

VS message

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

enter image description here

祝你好运

3 个答案:

答案 0 :(得分:7)

class ListUtilities

默认情况下,类是internal,只能在同一个程序集中查看 - 创建类public并且它应该可以正常工作:

public class ListUtilities

答案 1 :(得分:1)

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

答案 2 :(得分:1)

你需要让你上课

public class ListUtilities

因为条款是通过defualt internal

旁注:

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