프로그래밍 노트

C# | DLL의 클래스를 Remoting으로 불러오기 본문

C#/기초

C# | DLL의 클래스를 Remoting으로 불러오기

떡잎 2012. 12. 12. 19:42

먼저 간단한 DLL을 만든다.

DLL을 만들 때에는 C#에서는 클래스 라이브러리로 만든다.

프로젝트를 하나 만드는데 클래스 라이브러리로 만든다.

네임스페이스는 각자 프로젝트 이름에 따라 달라짐으로 소스를 볼때 주의가 필요하다.

using System;


namespace LunchCompo
{
 
    public class Lunch
    {
        string[] MyMenu = { "짬뽕", "초밥", "순대" };

        public string TodayMenu()
        {
            Random random = new Random();
            int randomNumber = random.Next(0, MyMenu.Length - 1);

            return MyMenu[randomNumber];
        }
    }
}


그리고 같은 솔루션에서 오른쪽 클릭을 하여 추가 > 새 프로젝트를 선택하여 콘솔 응용 프로그램을 추가한다.

그리고 아래의 소스를 참고해서 소스를 입력한다.


using System;

// Remoting에 사용되는 것들
// Remote를 사용하기 위해서는 참조에서
// System.Runtime.Remoting를 추가해야 된다.
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

// LunchCompo를 using으로 사용하려면 LunchCompo를 
// 먼저 빌드한 후 참조에서 LunchCompo를 추가해야 된다. 
using LunchCompo;

namespace RemotingDll
{
    class Program
    {
        static void Main(string[] args)
        {
            Lunch myMeal = new Lunch();

            //리모팅 객체인지 확인
            if (RemotingServices.IsTransparentProxy(myMeal))
                Console.WriteLine("원격 객체");
            else
                Console.WriteLine("로컬 객체");

            Console.WriteLine("오늘 점심 메뉴는 {0} ",myMeal.TodayMenu());
                           
            Console.ReadLine();

        }
    }
}


네임스페이스에 주의하면 위 소스를 추가하면 using System.Runtime.Remoting.Channels.Tcp와
using LunchCompo에 빨간 줄이 간다. 그것은 참조 추가에서 추가한다.
RemotingDll의 프로젝트의 참조에서 using System.Runtime.Remoting.Channels.Tcp는 닷넷 탭에서 추가하고 using LunchCompo는 프로젝트 탭에서 추가한다.

이렇게 하면 DLL의 클래스의 메서드를 RemotingDll에서 Remoting 으로 호출하여 값을 가져온다.





Comments