示例#1
0
        [NotNull] private static Dictionary <string, Type> BuildTypeNameCache()
        {
            var outp = new Dictionary <string, Type>();

            var everything = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in everything)
            {
                var name = assembly.GetName().Name;
                if (name == null || name.StartsWith("System."))
                {
                    continue;                                             // skip anything we know is not going to contain contract types
                }
                var types = assembly.GetTypes().Where(t => t.IsPublic && t.IsInterface);
                foreach (var interf in types)
                {
                    var shortName = InterfaceStack.Shorten(interf);
                    if (outp.ContainsKey(shortName))
                    {
                        outp[shortName] = null;                              // null out on conflict, as a feedback mechanism
                    }
                    else
                    {
                        outp.Add(shortName, interf);
                    }
                }
            }

            return(outp);
        }
        ///<summary>Return a JSON string representing a source object</summary>
        public string Serialise(object messageObject, out string typeDescription)
        {
            if (messageObject == null)
            {
                throw new ArgumentNullException(nameof(messageObject));
            }
            var type       = messageObject.GetType();
            var interfaces = type.DirectlyImplementedInterfaces()?.ToList();

            if (interfaces == null || !interfaces.HasSingle())
            {
                throw new ArgumentException("Messages must directly implement exactly one interface", "messageObject");
            }

            typeDescription = InterfaceStack.Of(messageObject);
            return(Json.Freeze(messageObject));
        }