简体   繁体   中英

Compiling class in DLL and referencing it in another project in C#

Hope I'm asking this correctly.

I have this class:

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);
    }
}

and I want to compile it in a .DLL file. I have tried doing it by the console like this:

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

and putting that class in a Class Library project and building it, and I get the DLL file in both cases but the problem comes when I want to use that DLL.

I create a new Project, and I do reference the DLL file, but when I want to use it, this is the problem:

VS消息

And, seems that there is nothing inside the DLL:

在此处输入图片说明

Best regards

class ListUtilities

默认情况下,类是internal ,并且只能从同一程序集中看到-将您的类public ,并且应该可以正常工作:

public class ListUtilities

Your class needs to be public. By default classes are internal which means they can only be seen inside of the DDLL.

You need to make you class public

public class ListUtilities

as clases are by defualt internal .

On a side note:

You may try to use Visual Studio instead of Command Line which will make things easier for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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