Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取通过套接字接收的对象的类型_C#_Winforms_Sockets - Fatal编程技术网

C# 获取通过套接字接收的对象的类型

C# 获取通过套接字接收的对象的类型,c#,winforms,sockets,C#,Winforms,Sockets,我有一个客户机/服务器应用程序。我使用了一个名为MessageHolder的类,该类接受一个获取send的对象。我有公司、合同、联系人等类,可以通过此MessageHolder类发送。现在,当我的服务器接收到MessageHolder时,如何获取其中包含的对象的类型 消息持有者类别: [Serializable] public class MessageHolder { public object company { get; set; } public CompanyCreat

我有一个客户机/服务器应用程序。我使用了一个名为MessageHolder的类,该类接受一个获取send的对象。我有公司、合同、联系人等类,可以通过此MessageHolder类发送。现在,当我的服务器接收到MessageHolder时,如何获取其中包含的对象的类型

消息持有者类别:

[Serializable]
public class MessageHolder
{
    public object company { get; set; }
    public CompanyCreationClass(object Company)
    {
        company = Company;
    }
}
方法如下:

System.Type t = company.GetType()

我会提出一些建议。 您可能会发现泛型比传递“对象”更好

公共类MyGenericHolder
{
公共MyGenericHolder()
{
}
私有T_ittem=默认值(T);
公共无效推送(T项)
{
此项为项目;
}
公共广播电台
{
把这个还给我;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
MyGenericHolder intHolder=新的MyGenericHolder();
推力(101);
intx=intHolder.Pop();
控制台写入线(x);
MyGenericHolder stringHolder=新的MyGenericHolder();
stringHolder.Push(“Hello泛型”);
字符串y=stringHolder.Pop();
控制台写入线(y);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
控制台。写入线(“按回车键”);
Console.ReadLine();
}
}

typeof(MessageHolderInstance)?这个很好用,谢谢。这个可能有用,但不知道如何使用。因为我必须做一个if语句来检查它<代码>公司类公司=新公司类()。但我的新公司类需要参数。如果我不想添加参数,这就很难创建新实例。
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();
    }
}
MessageHolder obj = new  MessageHolder(); 
Type t = typeof(obj);

// Alternatively, you could use 
MessageHolder obj = new  MessageHolder(); 
Type t = obj.GetType();