示例#1
0
            public static CanConvertType GetCanConvertType(Type t)
            {
                if (t == null)
                {
                    return(null);
                }

                bool hadNullable = false;
                Type t1          = t.GetUnderlyingTypeIfNullable();

                if (t1 != null)
                {
                    // The input type is Nullable-T, reset t to the generic type it wraps.
                    // The caller of this method will still have the original nullable type itself
                    // and will set both types as entries in the Dictionary both pointing to this
                    // new CanConvertType instance
                    t           = t1;
                    hadNullable = true;
                }

                var c = new CanConvertType()
                {
                    type = t,
                    IsStringSerializable = typeof(IStringSerializable).IsAssignableFrom(t),
                    HadNullable          = hadNullable
                };

                if (c.IsStringSerializable)
                {
                    c.StringSerializableInstance = Activator.CreateInstance(t) as IStringSerializable;
                }

                return(c);
            }
示例#2
0
        public override bool CanConvert(Type t)
        {
            CanConvertType c;

            if (_canConvertTypesDict.TryGetValue(t, out c))
            {
                return(c.IsStringSerializable);
            }

            c = CanConvertType.GetCanConvertType(t);

            _canConvertTypesDict[t] = c;             // note 't' might still be the nullable type, if so write another dict key below of main type

            if (c.HadNullable)
            {
                _canConvertTypesDict[c.type] = c;
            }

            return(c.IsStringSerializable);
        }