0

クライアント/サーバー アプリケーションがあります。送信されるオブジェクトを受け取る MessageHolder というクラスを使用します。この MessageHolder クラスで送信できる会社、契約、連絡先などのクラスがあります。サーバーが MessageHolder を受信すると、そこに含まれるオブジェクトのタイプを取得するにはどうすればよいですか?

メッセージ ホルダー クラス:

[Serializable]
public class MessageHolder
{
    public object company { get; set; }
    public CompanyCreationClass(object Company)
    {
        company = Company;
    }
}
4

2 に答える 2

0

メソッドは次のとおりです。

System.Type t = company.GetType()

http://msdn.microsoft.com/en-us/library/system.object.gettype.aspx

私は何かを提案します。「オブジェクト」を渡すよりもジェネリックの方が優れている場合があります。

public class MyGenericHolder<T>
{
    public MyGenericHolder()
    {
    }

    private T _theItem = default(T) ;

    public void Push(T item)
    {
        this._theItem = item;
    }

    public T Pop()
    {
        return this._theItem;
    }
}




class Program
{
    static void Main(string[] args)
    {

        try
        {

            MyGenericHolder<int> intHolder = new MyGenericHolder<int>();
            intHolder.Push(101);
            int x = intHolder.Pop();
            Console.WriteLine(x);

            MyGenericHolder<string> stringHolder = new MyGenericHolder<string>();
            stringHolder.Push("Hello Generics");
            string y = stringHolder.Pop();
            Console.WriteLine(y);


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


        Console.WriteLine("Press Enter");
        Console.ReadLine();
    }
}
于 2013-09-04T19:12:39.010 に答える