private static string ResolveNamespace(dnlib.DotNet.TypeDef source) { // If the namespace is present.. if (!string.IsNullOrEmpty(source.Namespace) && !source.IsNested) { // return the namespace return(source.Namespace); } // Otherwise.. // Get the source name var fullName = source.FullName.AsSpan(); // Find the index of the nested delimiter var nestedIndex = fullName.IndexOf('/'); // If the delimiter was found.. if (nestedIndex != -1) { // remove it fullName = fullName.Slice(0, nestedIndex); } //// Find the last delimiter before the type name //var dotIndex = fullName.LastIndexOf('.'); //// Remove the type name from the namespace //var result = fullName.Slice(0, dotIndex); // Return the result return(fullName.ToString()); }
private static IEnumerable <IResType> ResolveBaseClass(dnlib.DotNet.TypeDef source, Resolver resolver, out IResType?result) { // If the base type is not an object.. result = source.BaseType?.FullName.Equals("System.Object", StringComparison.InvariantCulture) == false // resolve the base type ? resolver.Resolve(source.BaseType.ToTypeSig(), source.ResolveTypeGenerics()) // otherwise return a null base type : null; return(result == null ? Enumerable.Empty <IResType>() : new[] { result }); }
/// <summary> /// Default constructor /// </summary> /// <param name="resolver">Type resolver instance</param> /// <param name="source">Type source</param> /// <param name="parent">Nested type parent</param> internal ClassDef(Resolver resolver, dnlib.DotNet.TypeDef source, dnlib.DotNet.TypeDef?parent, DotNetType type) : base(resolver, source, parent, ResolveGenerics(resolver, source, parent), ResolveBaseClass(source, resolver, out var baseType), type) { // If the source is null.. if (source is null) { // throw an exception throw new ArgumentNullException(nameof(source)); } // Initialize the base class BaseClass = baseType; // Initialize the constructors Constructors = source.Methods // Select valid constructors .Where(methodDef => !methodDef.SemanticsAttributes.HasFlag(MethodSemanticsAttributes.Getter) && !methodDef.SemanticsAttributes.HasFlag(MethodSemanticsAttributes.Setter) && !methodDef.IsPrivate && methodDef.IsConstructor) // Initialize constructors .Select(x => new ConstructorDef(resolver, x, parent != null)) // Materialize the collection .ToReadOnlyCollection(); // Determine whether this type is static IsStatic = source.IsSealed && source.IsAbstract; // If the type is static.. if (IsStatic) { // then it is not sealed IsSealed = false; // and not abstract IsAbstract = false; } // Otherwise.. else { // then it can be sealed IsSealed = source.IsSealed; // and it can be abstract IsAbstract = source.IsAbstract; } }
/// <summary> /// Default constructor /// </summary> /// <param name="resolver">Type resolver instance</param> /// <param name="source">Type source</param> /// <param name="parent">Nested type parent</param> internal StructDef(Resolver resolver, dnlib.DotNet.TypeDef source, dnlib.DotNet.TypeDef?parent, DotNetType type) : base(resolver, source, parent, ResolveGenericStructs(resolver, source, parent), Enumerable.Empty <IResType>(), type) { IsReadOnly = source .CustomAttributes .Any(x => x.TypeFullName.Equals("System.Runtime.CompilerServices.IsReadOnlyAttribute", StringComparison.InvariantCultureIgnoreCase)); // Initialize the constructors Constructors = source.Methods // Select valid constructors .Where(methodDef => !methodDef.SemanticsAttributes.HasFlag(MethodSemanticsAttributes.Getter) && !methodDef.SemanticsAttributes.HasFlag(MethodSemanticsAttributes.Setter) && !methodDef.IsPrivate && methodDef.IsConstructor) // Initialize constructors .Select(x => new ConstructorDef(resolver, x, parent != null)) // Materialize the collection .ToReadOnlyCollection(); }
private static AccessorType ResolveAccessor(dnlib.DotNet.TypeDef type) { // If the type is public.. if (type.Visibility == TypeAttributes.Public || (type.IsNested && type.Visibility == TypeAttributes.NestedPublic)) { // return public return(AccessorType.Public); } // If the type is nested protected if (type.Visibility == TypeAttributes.NestedFamily) { // return protected return(AccessorType.Protected); } // If the type is nested protected internal if (type.Visibility == TypeAttributes.NestedFamORAssem) { // return protected internal return(AccessorType.ProtectedInternal); } // Otherwise return internal return(AccessorType.Internal); }
/// <summary> /// Default constructor /// </summary> /// <param name="resolver">Type resolver instance</param> /// <param name="source">Type source</param> /// <param name="parent">Nested type parent</param> protected internal TypeDef(Resolver resolver, dnlib.DotNet.TypeDef source, dnlib.DotNet.TypeDef?parent, DotNetType type) { // If the source is null.. if (source is null) { // throw an exception throw new ArgumentNullException(nameof(source)); } // Initialize the accessor Accessor = ResolveAccessor(source); // Initialize the namespace TypeNamespace = ResolveNamespace(source); // Initialize the name Name = ResolveName(source, parent); // Initialize the raw name RawName = source.ReflectionFullName.Replace('+', '.'); // Initialize the resolver Resolver = resolver; // Initialize the is nested indicator IsNested = parent is not null; Type = type; }
private static IReadOnlyDictionary <string, (Variance variance, IReadOnlyCollection <IResType>)> ResolveGenericStructs(Resolver resolver, dnlib.DotNet.TypeDef source, dnlib.DotNet.TypeDef?parent) { IResType ResolveType(GenericParamConstraint x, IReadOnlyDictionary <string, string> generics) => resolver.Resolve(x.Constraint.ToTypeSig(), generics); (Variance, IReadOnlyCollection <IResType>) ResolveParameter(GenericParam parameter, IReadOnlyDictionary <string, string> generics) { // If the parameter has no generic constraints.. if (!parameter.HasGenericParamConstraints) { // return default return(Variance.NonVariant, Enumerable.Empty <IResType>().ToArray()); } // Otherwise return the list of constraints return(Variance.NonVariant, parameter.GenericParamConstraints.Select(x => ResolveType(x, generics)).ToReadOnlyCollection()); } return(source.GenericParameters // Exclude generic parameters from the parent .Except(parent?.GenericParameters ?? Enumerable.Empty <GenericParam>(), EqualityComparerEx <GenericParam> .Create(x => x.Name, x => x.Name)) // Materialize the result as a dictionary .ToDictionary(param => param.Name.String, param => ResolveParameter(param, source.ResolveTypeGenerics()))); }