簡體   English   中英

在DLL中編譯類並在C#中的另一個項目中引用它

[英]Compiling class in DLL and referencing it in another project in C#

希望我正確地問這個。

我有這個課:

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消息

而且,似乎DLL中沒有任何內容:

在此處輸入圖片說明

最好的祝福

class ListUtilities

默認情況下,類是internal ,並且只能從同一程序集中看到-將您的類public ,並且應該可以正常工作:

public class ListUtilities

您的課程需要公開。 默認情況下,類是內部類,這意味着它們只能在DDLL內部看到。

您需要公開課

public class ListUtilities

因為句法是通過內部的defualt。

附帶說明:

您可以嘗試使用Visual Studio而不是命令行來簡化操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM