Example #1
0
 static partial void ValidateObjectExtension(ref ValidationErrorsBuilder e, NamespaceSignature obj)
 {
     if (obj.Parent is null && !string.IsNullOrEmpty(obj.Name))
     {
         e.Add(ValidationErrors.Create($"Namespace {obj.Name} must have a parent (did you intent to use NamespaceSignature.Global as a parent?)").Nest("parent"));
     }
     if (string.IsNullOrEmpty(obj.Name) && obj.Parent is object)
     {
         e.Add(ValidationErrors.Create($"Namespace in {obj.Parent} must have a non-empty name.").Nest("name"));
     }
 }
Example #2
0
        /// <summary> Gets a <see cref="TypeSignature"/> of the specified reflection <se cref="System.Type" />. If the type is generic, it must be the definition without the generic parameters instantiated. All the important metadata is copied from the reflection type, it can be used on any type even though it may not be valid in the specific <see cref="MetadataContext" />. </summary>
        public static TypeSignature FromType(Type type)
        {
            // Assert.True(!type.IsGenericType || type.IsGenericTypeDefinition);
            if (type.IsArray)
            {
                throw new ArgumentException($"Can not create TypeSignature from array ({type})", nameof(type));
            }
            if (type.IsByRef)
            {
                throw new ArgumentException($"Can not create TypeSignature from reference ({type})", nameof(type));
            }
            if (type.IsPointer)
            {
                throw new ArgumentException($"Can not create TypeSignature from pointer ({type})", nameof(type));
            }

            if (type.IsGenericType && !type.IsGenericTypeDefinition)
            {
                type = type.GetGenericTypeDefinition();
            }

            var parent = type.DeclaringType is object?FromType(type.DeclaringType) :
                             type.Namespace is null       ? (TypeOrNamespace)NamespaceSignature.Global :
                             (TypeOrNamespace)NamespaceSignature.Parse(type.Namespace);

            var parentGenericArgs = type.DeclaringType?.GetGenericArguments().Length ?? 0;
            var kind =
                type.IsEnum ? "enum" :
                type.IsValueType ? "struct" :
                type.IsInterface ? "interface" :
                typeof(MulticastDelegate) == type.BaseType ? "delegate" :
                type.IsClass ? "class" :
                throw new NotSupportedException($"Can not translate {type} to TypeSignature");
            var accessibility = type.IsPublic || type.IsNestedPublic ? Accessibility.APublic :
                                type.IsNestedAssembly ? Accessibility.AInternal :
                                type.IsNestedPrivate ? Accessibility.APrivate :
                                type.IsNestedFamily ? Accessibility.AProtected :
                                type.IsNestedFamORAssem ? Accessibility.AProtectedInternal :
                                type.IsNestedFamANDAssem ? Accessibility.APrivateProtected :
                                Accessibility.AInternal;
            var typeName = type.Name.Contains('`') ? type.Name.Substring(0, type.Name.IndexOf("`", StringComparison.Ordinal)) :
                           type.Name;

            return(new TypeSignature(typeName, parent, kind, type.IsValueType, !type.IsSealed, type.IsAbstract, accessibility, type.GetGenericArguments().Skip(parentGenericArgs).Select(GenericParameter.FromType).ToImmutableArray()));
        }
Example #3
0
 /// <summary> Finds ILSpy's INamespace based on ExprCS NamespaceSignature. The INamespace has access to the module contents, so it's much stronger than NamespaceSignature. </summary>
 internal INamespace GetNamespace(NamespaceSignature ns) =>
 ns == NamespaceSignature.Global ? Compilation.RootNamespace :
 GetNamespace(ns.Parent).GetChildNamespace(ns.Name) ?? throw new Exception($"Could not resolve namespace {ns}.");