public VariantEventPropertyGetterAnyWCast(VariantPropertyGetterCache propertyGetterCache,
     int assignedPropertyNumber, SimpleTypeCaster caster)
 {
     _propertyGetterCache = propertyGetterCache;
     _assignedPropertyNumber = assignedPropertyNumber;
     _caster = caster;
 }
 public VariantEventPropertyGetterAnyWCast(
     VariantEventType variantEventType,
     string propertyName,
     SimpleTypeCaster caster)
 {
     this.variantEventType = variantEventType;
     this.propertyName = propertyName;
     this.caster = caster;
 }
Exemple #3
0
        public void TestGetCaster()
        {
            Object[][] tests = new Object[][] {
                new object[] { typeof(long), 10, 10L },
                new object[] { typeof(double), 1, 1d },
                new object[] { typeof(int), 0x1, 1 },
                new object[] { typeof(float), 100, 100f },
                new object[] { typeof(int?), (short)2, 2 },
                new object[] { typeof(byte?), (short)2, (byte)2 },
                new object[] { typeof(short), (long)2, (short)2 },
            };

            for (int i = 0; i < tests.Length; i++)
            {
                SimpleTypeCaster caster = SimpleTypeCasterFactory.GetCaster(null, (Type)tests[i][0]);
                Assert.AreEqual(tests[i][2], caster.Invoke(tests[i][1]), "error in row:" + i);
            }

            Assert.AreEqual('A', SimpleTypeCasterFactory.GetCaster(typeof(string), typeof(char)).Invoke("ABC"));

            //Assert.AreEqual(BigInteger.ValueOf(100), SimpleTypeCasterFactory.GetCaster(typeof(long?), typeof(BigInteger)).Cast(100L));
            //Assert.AreEqual(100.0m, SimpleTypeCasterFactory.GetCaster(typeof(long?), typeof(BigDecimal)).Cast(100L));
            //Assert.AreEqual(new BigDecimal(100d), SimpleTypeCasterFactory.GetCaster(typeof(double?), typeof(BigDecimal)).Cast(100d));
        }
Exemple #4
0
 public void SetUp()
 {
     caster = SimpleTypeCasterFactory.GetCaster(typeof(ISupportA));
     //caster = new SimpleTypeCasterAnyType(typeof(ISupportA));
 }
 public NonnumericCasterComputer(SimpleTypeCaster numericTypeCaster)
 {
     _caster = numericTypeCaster;
 }
        public VariantPropertyDesc ResolveProperty(String propertyName, EventType[] variants)
        {
            bool existsInAll = true;
            Type commonType  = null;
            bool mustCoerce  = false;

            for (int i = 0; i < variants.Length; i++)
            {
                Type type = variants[i].GetPropertyType(propertyName); //.GetBoxedType();
                if (type == null)
                {
                    existsInAll = false;
                    continue;
                }

                if (commonType == null)
                {
                    commonType = type;
                    continue;
                }

                // compare types
                if (type == commonType)
                {
                    continue;
                }

                if (type.GetBoxedType() == commonType.GetBoxedType())
                {
                    commonType = commonType.GetBoxedType();
                    continue;
                }

                // coercion
                if (type.IsNumeric())
                {
                    if (TypeHelper.CanCoerce(type, commonType))
                    {
                        mustCoerce = true;
                        continue;
                    }
                    if (TypeHelper.CanCoerce(commonType, type))
                    {
                        mustCoerce = true;
                        commonType = type;
                    }
                }
                else if (commonType == typeof(Object))
                {
                    continue;
                }
                // common interface or base class
                else if (!type.IsBuiltinDataType())
                {
                    var supersForType = new FIFOHashSet <Type>();
                    TypeHelper.GetBase(type, supersForType);
                    supersForType.Remove(typeof(Object));

                    if (supersForType.Contains(commonType))
                    {
                        continue;   // type, or : common type
                    }
                    if (TypeHelper.IsSubclassOrImplementsInterface(commonType, type))
                    {
                        commonType = type;  // common type : type
                        continue;
                    }

                    // find common interface or type both implement
                    var supersForCommonType = new FIFOHashSet <Type>();
                    TypeHelper.GetBase(commonType, supersForCommonType);
                    supersForCommonType.Remove(typeof(Object));

                    // Take common classes first, ignoring interfaces
                    bool found = false;
                    foreach (Type superClassType in supersForType)
                    {
                        if (!superClassType.IsInterface && (supersForCommonType.Contains(superClassType)))
                        {
                            break;
                        }
                    }
                    if (found)
                    {
                        continue;
                    }
                    // Take common interfaces
                    foreach (var superClassType in supersForType)
                    {
                        if (superClassType.IsInterface && supersForCommonType.Contains(superClassType))
                        {
                            commonType = superClassType;
                            found      = true;
                            break;
                        }
                    }
                }

                commonType = typeof(Object);
            }

            if (!existsInAll)
            {
                return(null);
            }

            if (commonType == null)
            {
                return(null);
            }

            // property numbers should start at zero since the serve as array index
            var assignedPropertyNumber = currentPropertyNumber;

            currentPropertyNumber++;
            propertyGetterCache.AddGetters(assignedPropertyNumber, propertyName);

            EventPropertyGetter getter;

            if (mustCoerce)
            {
                SimpleTypeCaster caster = SimpleTypeCasterFactory.GetCaster(null, commonType);
                getter = new ProxyEventPropertyGetter
                {
                    ProcGet = eventBean =>
                    {
                        var variant        = (VariantEvent)eventBean;
                        var propertyGetter = propertyGetterCache.GetGetter(assignedPropertyNumber, variant.UnderlyingEventBean.EventType);
                        if (propertyGetter == null)
                        {
                            return(null);
                        }
                        var value = propertyGetter.Get(variant.UnderlyingEventBean);
                        if (value == null)
                        {
                            return(value);
                        }
                        return(caster.Invoke(value));
                    },
                    ProcGetFragment      = eventBean => null,
                    ProcIsExistsProperty = eventBean =>
                    {
                        var variant        = (VariantEvent)eventBean;
                        var propertyGetter = propertyGetterCache.GetGetter(assignedPropertyNumber, variant.UnderlyingEventBean.EventType);
                        if (propertyGetter == null)
                        {
                            return(false);
                        }
                        return(propertyGetter.IsExistsProperty(variant.UnderlyingEventBean));
                    }
                };
            }
            else
            {
                getter = new ProxyEventPropertyGetter
                {
                    ProcGet = eventBean =>
                    {
                        var variant        = (VariantEvent)eventBean;
                        var propertyGetter = propertyGetterCache.GetGetter(assignedPropertyNumber, variant.UnderlyingEventBean.EventType);
                        if (propertyGetter == null)
                        {
                            return(null);
                        }
                        return(propertyGetter.Get(variant.UnderlyingEventBean));
                    },
                    ProcGetFragment      = eventBean => null,
                    ProcIsExistsProperty = eventBean =>
                    {
                        var variant        = (VariantEvent)eventBean;
                        var propertyGetter = propertyGetterCache.GetGetter(assignedPropertyNumber, variant.UnderlyingEventBean.EventType);
                        if (propertyGetter == null)
                        {
                            return(false);
                        }
                        return(propertyGetter.IsExistsProperty(variant.UnderlyingEventBean));
                    }
                };
            }

            return(new VariantPropertyDesc(commonType, getter, true));
        }
Exemple #7
0
 public NumberCasterComputer(SimpleTypeCaster numericTypeCaster)
 {
     this.numericTypeCaster = numericTypeCaster;
 }