Example #1
0
		public static void VerifyType( Type targetType )
		{
			if ( targetType.GetIsInterface() || targetType.GetIsAbstract() )
			{
				throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType( targetType );
			}
		}
Example #2
0
		public static PropertyInfo FindEnumeratorCurrentProperty( Type enumeratorType, CollectionTraits traits )
		{
#if DEBUG
			Contract.Assert( traits.GetEnumeratorMethod != null );
#endif // DEBUG
			PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty( "Current" );

			if ( currentProperty == null )
			{
				if ( enumeratorType == typeof( IDictionaryEnumerator ) )
				{
					currentProperty = Metadata._IDictionaryEnumerator.Entry;
				}
				else if ( enumeratorType.GetIsInterface() )
				{
					if ( enumeratorType.GetIsGenericType() && enumeratorType.GetGenericTypeDefinition() == typeof( IEnumerator<> ) )
					{
						currentProperty = typeof( IEnumerator<> ).MakeGenericType( traits.ElementType ).GetProperty( "Current" );
					}
					else
					{
						currentProperty = Metadata._IEnumerator.Current;
					}
				}
			}
			return currentProperty;
		}
		public static bool Implements( this Type source, Type genericType )
		{
			Contract.Assert( source != null );
			Contract.Assert( genericType != null );
			Contract.Assert( genericType.GetIsInterface() );

			return EnumerateGenericIntefaces( source, genericType, false ).Any();
		}
		public static bool Implements( this Type source, Type genericType )
		{
#if !UNITY
			Contract.Assert( source != null, "source != null" );
			Contract.Assert( genericType != null, "genericType != null" );
			Contract.Assert( genericType.GetIsInterface(), "genericType.GetIsInterface()" );
#endif // !UNITY

			return EnumerateGenericIntefaces( source, genericType, false ).Any();
		}
		public static bool Implements( this Type source, Type genericType )
		{
#if !UNITY_ANDROID && !UNITY_IPHONE
			Contract.Assert( source != null );
			Contract.Assert( genericType != null );
			Contract.Assert( genericType.GetIsInterface() );
#endif // !UNITY_ANDROID && !UNITY_IPHONE

			return EnumerateGenericIntefaces( source, genericType, false ).Any();
		}
		/// <summary>
		///		Registers the default type of the collection.
		/// </summary>
		/// <param name="abstractCollectionType">Type of the abstract collection.</param>
		/// <param name="defaultCollectionType">Default concrete type of the <paramref name="abstractCollectionType"/>.</param>
		/// <exception cref="System.ArgumentNullException">
		///		<paramref name="abstractCollectionType"/> is <c>null</c>.
		///		Or <paramref name="defaultCollectionType"/> is <c>null</c>.
		/// </exception>
		/// <exception cref="System.ArgumentException">
		///		<paramref name="abstractCollectionType"/> is not collection type.
		///		Or <paramref name="defaultCollectionType"/> is abstract class or interface.
		///		Or <paramref name="defaultCollectionType"/> is open generic type but <paramref name="abstractCollectionType"/> is closed generic type.
		///		Or <paramref name="defaultCollectionType"/> is closed generic type but <paramref name="abstractCollectionType"/> is open generic type.
		///		Or <paramref name="defaultCollectionType"/> does not have same arity for <paramref name="abstractCollectionType"/>.
		///		Or <paramref name="defaultCollectionType"/> is not assignable to <paramref name="abstractCollectionType"/>
		///		or the constructed type from <paramref name="defaultCollectionType"/> will not be assignable to the constructed type from <paramref name="abstractCollectionType"/>.
		/// </exception>
		/// <remarks>
		///		If you want to overwrite default type for collection interfaces, you can use this method.
		///		Note that this method only supports collection interface, that is subtype of the <see cref="IEnumerable"/> interface.
		///		<note>
		///			If you register invalid type for <paramref name="defaultCollectionType"/>, then runtime exception will be occurred.
		///			For example, you register <see cref="IEnumerable{T}"/> of <see cref="Char"/> and <see cref="String"/> pair, but it will cause runtime error.
		///		</note>
		/// </remarks>
		/// <seealso cref="Get"/>
		public void Register( Type abstractCollectionType, Type defaultCollectionType )
		{

			if ( abstractCollectionType == null )
			{
				throw new ArgumentNullException( "abstractCollectionType" );
			}

			if ( defaultCollectionType == null )
			{
				throw new ArgumentNullException( "defaultCollectionType" );
			}

			// Use GetIntegerfaces() in addition to IsAssignableFrom because of open generic type support.
			if ( !abstractCollectionType.GetInterfaces().Contains( typeof( IEnumerable ) ) )
			{
				throw new ArgumentException(
					String.Format( CultureInfo.CurrentCulture, "The type '{0}' is not collection.", abstractCollectionType ),
					"abstractCollectionType" );
			}

			if ( abstractCollectionType.GetIsGenericTypeDefinition() )
			{
				if ( !defaultCollectionType.GetIsGenericTypeDefinition() )
				{
					throw new ArgumentException(
						String.Format(
							CultureInfo.CurrentCulture,
							"The closed generic type '{1}' cannot be assigned to open generic type '{0}'.",
							abstractCollectionType,
							defaultCollectionType ),
						"defaultCollectionType" );
				}

				if ( abstractCollectionType.GetGenericTypeParameters().Length !=
					defaultCollectionType.GetGenericTypeParameters().Length )
				{
					throw new ArgumentException(
						String.Format(
							CultureInfo.CurrentCulture,
							"The default generic collection type '{1}' does not have same arity for abstract generic collection type '{0}'.",
							abstractCollectionType,
							defaultCollectionType ),
						"defaultCollectionType" );
				}
			}
			else if ( defaultCollectionType.GetIsGenericTypeDefinition() )
			{
				throw new ArgumentException(
					String.Format(
						CultureInfo.CurrentCulture,
						"The open generic type '{1}' cannot be assigned to closed generic type '{0}'.",
						abstractCollectionType,
						defaultCollectionType ),
					"defaultCollectionType" );
			}

			if ( defaultCollectionType.GetIsAbstract() || defaultCollectionType.GetIsInterface() )
			{
				throw new ArgumentException(
					String.Format( CultureInfo.CurrentCulture, "The defaultCollectionType cannot be abstract class nor interface. The type '{0}' is abstract type.", defaultCollectionType ),
					"defaultCollectionType" );
			}

			// Use GetIntegerfaces() and BaseType instead of IsAssignableFrom because of open generic type support.
			if ( !abstractCollectionType.IsAssignableFrom( defaultCollectionType )
				 && abstractCollectionType.GetIsGenericTypeDefinition()
				 && !defaultCollectionType
						 .GetInterfaces()
						 .Select( t => ( t.GetIsGenericType() && !t.GetIsGenericTypeDefinition() ) ? t.GetGenericTypeDefinition() : t )
						 .Contains( abstractCollectionType )
				 && !IsAnscestorType( abstractCollectionType, defaultCollectionType ) )
			{
				throw new ArgumentException(
					String.Format(
						CultureInfo.CurrentCulture,
						"The type '{1}' is not assignable to '{0}'.",
						abstractCollectionType,
						defaultCollectionType ),
					"defaultCollectionType" );
			}

			this._defaultCollectionTypes.Register( abstractCollectionType, defaultCollectionType, /*allowOverwrite:*/ true );
		}
        protected ObjectReflectionMessagePackSerializer(Type type, SerializationContext context, SerializingMember[] members)
            : base(type, (context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions)
        {
            if (type.GetIsAbstract() || type.GetIsInterface())
            {
                throw SerializationExceptions.NewNotSupportedBecauseCannotInstanciateAbstractType(type);
            }

                this._createInstance = () => Activator.CreateInstance(type);

            //Expression.Lambda<Func<T>>(
            //    typeof(T).GetIsValueType()
            //        ? Expression.Default(typeof(T)) as Expression
            //        : Expression.New(typeof(T).GetConstructor(ReflectionAbstractions.EmptyTypes))
            //    ).Compile();
            var isPackable = typeof(IPackable).IsAssignableFrom(type);
            var isUnpackable = typeof(IUnpackable).IsAssignableFrom(type);

            if (isPackable && isUnpackable)
            {
                this._memberSerializers = null;
                this._indexMap = null;
                this._isCollection = null;
                this._nilImplications = null;
                this._memberNames = null;
            }
            else
            {
                this._memberSerializers =
                    members.Select(
                        m => m.Member == null ? NullSerializer.Instance : context.GetSerializer(m.Member.GetMemberValueType())).ToArray
                        (

                        );
                this._indexMap =
                    members
                        .Select((m, i) => new KeyValuePair<SerializingMember, int>(m, i))
                        .Where(kv => kv.Key.Member != null)
                        .ToDictionary(kv => kv.Key.Contract.Name, kv => kv.Value);

                this._isCollection =
                    members.Select(
                        m => m.Member == null ? CollectionTraits.NotCollection : m.Member.GetMemberValueType().GetCollectionTraits()).
                        Select(t => t.CollectionType != CollectionKind.NotCollection).ToArray();

                // NilImplication validity check
                foreach (var member in members)
                {
                    switch (member.Contract.NilImplication)
                    {
                        case NilImplication.Null:
                            {
                                if (member.Member.GetMemberValueType().GetIsValueType() &&
                                    Nullable.GetUnderlyingType(member.Member.GetMemberValueType()) == null)
                                {
                                    throw SerializationExceptions.NewValueTypeCannotBeNull(
                                        member.Contract.Name, member.Member.GetMemberValueType(), member.Member.DeclaringType
                                    );
                                }

                                if (!member.Member.CanSetValue())
                                {
                                    throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(member.Contract.Name);
                                }

                                break;
                            }
                    }
                }

                this._nilImplications = members.Select(m => m.Contract.NilImplication).ToArray();
                this._memberNames = members.Select(m => m.Contract.Name).ToArray();
            }

            if (isPackable)
            {
                this._packToMessage = (target, packer, packingOptions) =>
                {
                    ((IPackable)target).PackToMessage(packer, packingOptions);
                    //typeof(T).GetInterfaceMap(typeof(IPackable)).TargetMethods.Single().Invoke(target, new object[] { packer, packingOptions });
                };
                this._memberGetters = null;
            }
            else
            {
                this._packToMessage = null;
                this._memberGetters =
                    members.Select<SerializingMember,Func<object,object>>(
                    m => m.Member == null ? (target => null)
                        : CreateMemberGetter(m)).ToArray();
            }

            if (isUnpackable)
            {
                this._unpackFromMessage = delegate(ref object target, Unpacker value)
                {
                    ((IUnpackable)target).UnpackFromMessage(value);
                };

                this._memberSetters = null;
            }
            else
            {
                this._unpackFromMessage = null;

                this._memberSetters =
                    members.Select(
                        m =>
                        m.Member == null
                        ? delegate(ref object target, object memberValue) { }
                :
                  m.Member.CanSetValue()
                        ? CreateMemberSetter(m)
                        : UnpackHelpers.IsReadOnlyAppendableCollectionMember(m.Member)
                        ? default(MemberSetter)
                        : ThrowGetOnlyMemberIsInvalid(m.Member)
                ).ToArray();
            }
        }
		private static MethodInfo FindInterfaceMethod( Type targetType, Type interfaceType, string name, Type[] parameterTypes )
		{
			if ( targetType.GetIsInterface() )
			{
				return targetType.FindInterfaces( ( type, _ ) => type == interfaceType, null ).Single().GetMethod( name, parameterTypes );
			}

			var map = targetType.GetInterfaceMap( interfaceType );

#if !SILVERLIGHT
			int index = Array.FindIndex( map.InterfaceMethods, method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#else
			int index = map.InterfaceMethods.FindIndex( method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#endif

			if ( index < 0 )
			{
#if DEBUG
				Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join( ", ", parameterTypes.Select( t => t.ToString() ).ToArray() ) + ") is not found in " + targetType );
#endif
				return null;
			}
			else
			{
				return map.TargetMethods[ index ];
			}
		}
Example #9
0
		private static MethodInfo FindInterfaceMethod( Type targetType, Type interfaceType, string name, Type[] parameterTypes )
		{
			if ( targetType.GetIsInterface() )
			{
				return targetType.FindInterfaces( ( type, _ ) => type == interfaceType, null ).Single().GetMethod( name, parameterTypes );
			}

			var map = targetType.GetInterfaceMap( interfaceType );

#if !SILVERLIGHT || WINDOWS_PHONE
			int index = Array.FindIndex( map.InterfaceMethods, method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#else
			int index = map.InterfaceMethods.FindIndex( method => method.Name == name && method.GetParameters().Select( p => p.ParameterType ).SequenceEqual( parameterTypes ) );
#endif

			if ( index < 0 )
			{
#if DEBUG && !UNITY
#if !NETFX_35
				Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join<Type>( ", ", parameterTypes ) + ") is not found in " + targetType );
#else
				Contract.Assert( false, interfaceType + "::" + name + "(" + String.Join( ", ", parameterTypes.Select( t => t.ToString() ).ToArray() ) + ") is not found in " + targetType );
#endif // !NETFX_35
#endif // DEBUG && !UNITY
				// ReSharper disable once HeuristicUnreachableCode
				return null;
			}

			return map.TargetMethods[ index ];
		}
        public static bool CanHandleType(Type memberType)
        {
            if (memberType.GetIsInterface())
                return false;

            if (memberType.GetIsAbstract())
                return false;

            if (typeof(Delegate).IsAssignableFrom(memberType))
                return false;

            return true;
        }
		private static bool FilterCollectionType( Type type, object filterCriteria )
		{
#if !NETSTANDARD1_1 && !NETSTANDARD1_3
#if DEBUG
			Contract.Assert( type.GetIsInterface(), "type.IsInterface" );
#endif // DEBUG
			return type.GetAssembly().Equals( typeof( Array ).GetAssembly() ) && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#else
			var typeInfo = type.GetTypeInfo();
			Contract.Assert( typeInfo.IsInterface );
			return typeInfo.Assembly.Equals( typeof( Array ).GetTypeInfo().Assembly ) && ( type.Namespace == "System.Collections" || type.Namespace == "System.Collections.Generic" );
#endif // !NETSTANDARD1_1 && !NETSTANDARD1_3
		}