Beispiel #1
0
        /// <summary>
        /// Creates a closed generic nullable type from the current <see cref="CodeTypeReference"/>.
        /// </summary>
        /// <param name="reference">The code type reference.</param>
        /// <returns>A <see cref="Type"/> representation of the nullable type.</returns>
        /// <exception cref="InvalidOperationException">Thrown if the code type reference does not represent a nullable type.</exception>
        public static Type CloseNullableType(this CodeTypeReference reference)
        {
            if (!reference.IsNullableType())
            {
                throw new InvalidOperationException("The current code type reference is not a nullable type.");
            }
            Type valueType = Type.GetType(reference.TypeArguments[0].BaseType);

            return(typeof(Nullable <>).MakeGenericType(valueType));
        }
Beispiel #2
0
        private static CodeTypeDeclaration GenerateCollectionType(CodeTypeReference entityType)
        {
            string entityTypeName = entityType.IsNullableType()
                                ? "Nullable" + Type.GetType(entityType.TypeArguments[0].BaseType).Name
                                : entityType.BaseType;
            string collectionTypeName          = GetCollectionTypeName(entityTypeName);
            CodeTypeDeclaration collectionType = new CodeTypeDeclaration(collectionTypeName);
            CodeTypeReference   baseType       = new CodeTypeReference(typeof(Collection <>));

            if (entityType.IsNullableType())
            {
                baseType.TypeArguments.Add(entityType.CloseNullableType());
            }
            else
            {
                baseType.TypeArguments.Add(entityType.BaseType);
            }

            collectionType.BaseTypes.Add(baseType);
            return(collectionType);
        }
Beispiel #3
0
        public CodeTypeReference CreateCollectionType(CodeTypeReference entityType, ExtendedCodeDomTree code)
        {
            Debug.Assert(entityType != null, "Argument entity type could not be null.");

            // Do not create a ByteCollection as this is not compatiable with base64Binary!
            if (entityType.BaseType == typeof(byte).FullName)
            {
                return(entityType);
            }

            CodeTypeReference ctr = new CodeTypeReference(typeof(List <>));

            if (entityType.IsNullableType())
            {
                ctr.TypeArguments.Add(entityType.CloseNullableType());
            }
            else
            {
                ctr.TypeArguments.Add(entityType.BaseType);
            }
            return(ctr);
        }