예제 #1
0
        public static bool IsWoopsaValueType(WoopsaConverters customValueTypeConverters, Type type)
        {
            WoopsaValueType woopsaValueType;
            WoopsaConverter converter;

            return(InferWoopsaType(customValueTypeConverters, type,
                                   out woopsaValueType, out converter));
        }
 /// <summary>
 /// Create a WoopsaObjectAdapter for a fixed object reference
 /// </summary>
 /// <param name="container">Can be null if it's the root object.</param>
 /// <param name="name"></param>
 /// <param name="targetObjectGetter"></param>
 /// <param name="declaredExposedType">
 /// Type to expose for the targetObject.
 /// Specify null to use targetObject.GetType().
 /// The specified type can be different than the targetObject.GetType()
 /// when an interface is published instead of the public methods of the type.
 /// Specifying null implies that the type is determined dynamically, as targetObjectGetter
 /// does not necesarily return always the same object.
 /// </param>
 /// <param name="options"></param>
 /// <param name="defaultVisibility"></param>
 public WoopsaObjectAdapter(WoopsaContainer container, string name, object targetObject,
                            Type declaredExposedType = null,
                            WoopsaConverters customValueTypeConverters = null,
                            WoopsaObjectAdapterOptions options         = WoopsaObjectAdapterOptions.None,
                            WoopsaVisibility defaultVisibility         = DefaultDefaultVisibility) :
     this(container, name, () => targetObject, declaredExposedType,
          new TypeDescriptions(customValueTypeConverters),
          options, defaultVisibility)
 {
 }
예제 #3
0
        public static TypeDescription ReflectType(
            Type targetType,
            WoopsaConverters customValueTypeConverters = null)
        {
            TypeDescription typeDescription = new TypeDescription(targetType);

            ReflectProperties(targetType, typeDescription.Properties,
                              typeDescription.Items, customValueTypeConverters);
            ReflectMethods(targetType, typeDescription.Methods, customValueTypeConverters);
            return(typeDescription);
        }
예제 #4
0
 private static bool InferWoopsaType(WoopsaConverters customValueTypeConverters, Type type, out WoopsaValueType woopsaValueType, out WoopsaConverter converter)
 {
     if (customValueTypeConverters != null)
     {
         return(customValueTypeConverters.InferWoopsaType(type, out woopsaValueType, out converter));
     }
     else
     {
         converter = WoopsaConverterDefault.Default;
         return(WoopsaTypeUtils.InferWoopsaType(type, out woopsaValueType));
     }
 }
예제 #5
0
 public WoopsaConverters(WoopsaConverters converters = null)
 {
     if (converters != null)
     {
         _converterDescriptions = new Dictionary <Type, WoopsaConverterDescription>(
             converters._converterDescriptions);
     }
     else
     {
         _converterDescriptions = new Dictionary <Type, WoopsaConverterDescription>();
     }
 }
예제 #6
0
        public static void ReflectProperties(
            Type targetType,
            PropertyDescriptions propertyDescriptions,
            ItemDescriptions itemDescriptions,
            WoopsaConverters customValueTypeConverters = null)
        {
            PropertyInfo[] properties;
            if (!targetType.IsInterface)
            {
                properties = targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
            else
            {
                properties = (new Type[] { targetType }).Concat(targetType.GetInterfaces()).SelectMany(i => i.GetProperties()).ToArray();
            }
            foreach (var propertyInfo in properties)
            {
                WoopsaValueTypeAttribute attribute = GetCustomAttribute <WoopsaValueTypeAttribute>(propertyInfo);

                bool isValidWoopsaProperty = false;
                isValidWoopsaProperty = InferWoopsaType(customValueTypeConverters, propertyInfo.PropertyType,
                                                        out WoopsaValueType woopsaPropertyType,
                                                        out WoopsaConverter converter);
                if (attribute != null)
                {
                    woopsaPropertyType    = attribute.ValueType;
                    isValidWoopsaProperty = true;
                }
                if (isValidWoopsaProperty)
                {
                    //This property is a C# property of a valid basic Woopsa Type, it can be published as a Woopsa property
                    PropertyDescription newPropertyDescription = new PropertyDescription(
                        woopsaPropertyType, propertyInfo,
                        !propertyInfo.CanWrite || propertyInfo.GetSetMethod(false) == null,
                        converter);
                    propertyDescriptions.Add(newPropertyDescription);
                }
                else if (!propertyInfo.PropertyType.IsValueType)
                {
                    // This property is not of a WoopsaType, if it is a reference type, assume it is an inner item
                    ItemDescription newItem = new ItemDescription(propertyInfo);
                    itemDescriptions.Add(newItem);
                }
            }
        }
예제 #7
0
 public WoopsaDynamicClientObject(WoopsaConverters customTypeConverters = null)
 {
     CustomTypeConverters = customTypeConverters;
 }
예제 #8
0
 public WoopsaDynamicClient(WoopsaClient client, WoopsaConverters customTypeConverters = null) : base(customTypeConverters)
 {
     _client = client;
     Refresh();
 }
예제 #9
0
 public WoopsaDynamicClient(string url, WoopsaConverters customTypeConverters = null) : this(new WoopsaClient(url), customTypeConverters)
 {
 }
예제 #10
0
        public static void ReflectMethods(
            Type targetType,
            MethodDescriptions methodDescriptions,
            WoopsaConverters customValueTypeConverters = null)
        {
            const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;

            List <MethodInfo> methods = new List <MethodInfo>(targetType.GetMethods(flags));

            foreach (Type inter in targetType.GetInterfaces())
            {
                foreach (MethodInfo method in inter.GetMethods(flags))
                {
                    if (!methods.Contains(method))
                    {
                        methods.Add(method);
                    }
                }
            }
            foreach (var methodInfo in methods)
            {
                WoopsaConverter          converter;
                WoopsaValueTypeAttribute attribute = GetCustomAttribute <WoopsaValueTypeAttribute>(methodInfo);
                WoopsaValueType          woopsaReturnType;
                bool isValidWoopsaMethod = false;
                isValidWoopsaMethod = InferWoopsaType(customValueTypeConverters, methodInfo.ReturnType, out woopsaReturnType, out converter);
                if (attribute != null)
                {
                    woopsaReturnType    = attribute.ValueType;
                    isValidWoopsaMethod = true;
                }
                if (isValidWoopsaMethod)
                {
                    bool argumentsTypeCompatible   = true;
                    ArgumentDescriptions arguments = new ArgumentDescriptions();
                    int parameterIndex             = 0;
                    foreach (var parameter in methodInfo.GetParameters())
                    {
                        WoopsaConverter argumentConverter;
                        WoopsaValueType argumentType;
                        if (InferWoopsaType(customValueTypeConverters, parameter.ParameterType, out argumentType, out argumentConverter))
                        {
                            string parameterName;
                            parameterName = parameter.Name;
                            if (string.IsNullOrEmpty(parameterName))
                            {
                                if (typeof(Array).IsAssignableFrom(targetType))
                                {
                                    if (methodInfo.Name == "Set")
                                    {
                                        if (parameterIndex == 0)
                                        {
                                            parameterName = "index";
                                        }
                                        else if (parameterIndex == 1)
                                        {
                                            parameterName = "value";
                                        }
                                    }
                                    else if (methodInfo.Name == "Get")
                                    {
                                        if (parameterIndex == 0)
                                        {
                                            parameterName = "index";
                                        }
                                    }
                                }
                                if (parameterName == null)
                                {
                                    parameterName = "p" + parameterIndex.ToString();
                                }
                            }
                            ArgumentDescription newArgument = new ArgumentDescription(
                                new WoopsaMethodArgumentInfo(parameterName, argumentType),
                                parameter.ParameterType, argumentConverter);
                            arguments.Add(newArgument);
                        }
                        else
                        {
                            argumentsTypeCompatible = false;
                            break;
                        }
                        parameterIndex++;
                    }
                    if (argumentsTypeCompatible)
                    {
                        MethodDescription newMethod = new MethodDescription(
                            woopsaReturnType, arguments, methodInfo, converter);
                        methodDescriptions.Add(newMethod);
                    }
                }
            }
        }
예제 #11
0
 public TypeDescriptions(WoopsaConverters customValueTypeConverters)
 {
     _typeDescriptions    = new Dictionary <Type, TypeDescription>();
     CustomTypeConverters = customValueTypeConverters;
 }