コード例 #1
0
ファイル: Program.cs プロジェクト: manzik/Communicator
        static void Main(string[] args)
        {
            CommunicatorOptions co = new CommunicatorOptions();

            co.password = "******";

            Communicator communicator = new Communicator(co);

            communicator.listen(1234);

            communicator.On("TestMessage", data => {
                Message msg = new Message();

                CommunicatorTools.CopyToObject(msg, data);


                Console.WriteLine("A program from " + msg.lang + " " + msg.langversion + " on " + msg.os + " " + msg.osversion + " connected");

                File.WriteAllBytes("ReceivedImage.png", msg.senderimg);

                Console.WriteLine("An image was received and saved to: ReceivedImage.png");

                Console.WriteLine("Received a message:");

                Console.WriteLine(msg.msg);
            });

            Console.ReadKey();
        }
コード例 #2
0
        public static void CopyToObject(dynamic inobj, object sourceobj)
        {
            string[] properties, sourceproperties;

            properties       = CommunicatorTools.GetPropertiesFromObject(inobj);
            sourceproperties = CommunicatorTools.GetPropertiesFromObject(sourceobj);

            properties = new List <string>(properties).Intersect(new List <string>(sourceproperties)).ToArray();

            for (int i = 0; i < properties.Length; i++)
            {
                string  property     = properties[i];
                dynamic targetsource = CommunicatorTools.GetFromObjectByProperty(sourceobj, property);
                dynamic targetin     = CommunicatorTools.GetFromObjectByProperty(inobj, property);

                if (CommunicatorTools.ValueType(targetin) > 0 && (CommunicatorTools.ValueType(targetin) == CommunicatorTools.ValueType(targetsource) || CommunicatorTools.ValueType(targetsource) == 5))
                {
                    if (CommunicatorTools.ValueType(targetsource) > 0)
                    {
                        dynamic arr = null;
                        if (targetsource.GetType().IsArray)
                        {
                            arr = Array.CreateInstance((targetin).GetType().GetElementType(), ((dynamic)targetsource).Length);
                            for (int j = 0; j < arr.Length; j++)
                            {
                                arr.SetValue(Convert.ChangeType(targetsource[j], (targetin).GetType().GetElementType()), j);
                            }
                        }
                        if (inobj is ExpandoObject)
                        {
                            ((IDictionary <string, object>)inobj)[property] = targetsource;
                        }
                        else
                        {
                            ((object)inobj).GetType()
                            .GetFields(BindingFlags.Public | BindingFlags.NonPublic |
                                       BindingFlags.Static | BindingFlags.Instance |
                                       BindingFlags.DeclaredOnly)
                            .ToList()
                            .Find(f => f.Name == property).SetValue(inobj, targetsource.GetType().IsArray ? arr : (targetsource is double?Convert.ChangeType(targetsource, Convert.GetTypeCode(targetin)) : targetsource));
                        }
                    }
                    else
                    {
                        CopyToObject(targetin, targetsource);
                    }
                }
            }
        }