Skip to content

Commit

Permalink
Add GetWellKnownType()
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Oct 22, 2017
1 parent b9ac01b commit bb19bf7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,18 @@ public bool IsPrimitive {
/// </summary>
public bool IsMarshalByRef => CanCastTo(AppDomain.GetWellKnownType(DmdWellKnownType.System_MarshalByRefObject, isOptional: true));

/// <summary>
/// Gets the <see cref="DmdWellKnownType"/> value or <see cref="DmdWellKnownType.None"/> if it's not a well known type
/// </summary>
/// <returns></returns>
public DmdWellKnownType GetWellKnownType() {
if (!DmdWellKnownTypeUtils.TryGetWellKnownType(DmdTypeName.Create(this), out var wkt))
return DmdWellKnownType.None;
if (this != AppDomain.GetWellKnownType(wkt, isOptional: true))
return DmdWellKnownType.None;
return wkt;
}

/// <summary>
/// Gets the element type if it's an array, a by-ref or a pointer type
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ public enum DmdWellKnownType {
System_Linq_SystemCore_EnumerableDebugViewEmptyException,
System_Text_Encoding,

// When adding more types, update DmdWellKnownTypeUtils

// When adding more types, update WellKnownMemberResolver
None = -1,
#pragma warning restore 1591 // Missing XML comment for publicly visible type or member
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,25 @@ public static bool TryGetWellKnownType(DmdTypeName name, out DmdWellKnownType we
/// </summary>
/// <param name="wellKnownType">Well known type</param>
/// <returns></returns>
public static DmdTypeName GetTypeName(DmdWellKnownType wellKnownType) =>
toWellKnownTypeName[(int)wellKnownType];
public static DmdTypeName GetTypeName(DmdWellKnownType wellKnownType) {
Debug.Assert(wellKnownType != DmdWellKnownType.None);
return toWellKnownTypeName[(int)wellKnownType];
}

static void Add(DmdTypeName typeName, DmdWellKnownType wellKnownType) {
Debug.Assert(wellKnownType != DmdWellKnownType.None);
toWellKnownType.Add(typeName, wellKnownType);
toWellKnownTypeName[(int)wellKnownType] = typeName;
}

/// <summary>
/// Gets the number of well known types
/// </summary>
public static int NumberOfWellKnownTypes => 282;
public static int WellKnownTypesCount => 282;

static DmdWellKnownTypeUtils() {
toWellKnownType = new Dictionary<DmdTypeName, DmdWellKnownType>(NumberOfWellKnownTypes, DmdTypeNameEqualityComparer.Instance);
toWellKnownTypeName = new DmdTypeName[NumberOfWellKnownTypes];
toWellKnownType = new Dictionary<DmdTypeName, DmdWellKnownType>(WellKnownTypesCount, DmdTypeNameEqualityComparer.Instance);
toWellKnownTypeName = new DmdTypeName[WellKnownTypesCount];

Add(new DmdTypeName("System", "Object"), DmdWellKnownType.System_Object);
Add(new DmdTypeName("System", "Enum"), DmdWellKnownType.System_Enum);
Expand Down Expand Up @@ -342,7 +345,7 @@ static DmdWellKnownTypeUtils() {
Add(new DmdTypeName("System.Linq", "SystemCore_EnumerableDebugViewEmptyException"), DmdWellKnownType.System_Linq_SystemCore_EnumerableDebugViewEmptyException);
Add(new DmdTypeName("System.Text", "Encoding"), DmdWellKnownType.System_Text_Encoding);

Debug.Assert(toWellKnownType.Count == NumberOfWellKnownTypes);
Debug.Assert(toWellKnownType.Count == WellKnownTypesCount);
#if DEBUG
foreach (var name in toWellKnownTypeName)
Debug.Assert(name.Name != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public WellKnownMemberResolver(DmdAppDomain appDomain) {
lockObj = new object();
checkedModules = new HashSet<DmdModule>();
this.appDomain = appDomain ?? throw new ArgumentNullException(nameof(appDomain));
wellKnownTypes = new DmdType[DmdWellKnownTypeUtils.NumberOfWellKnownTypes];
wellKnownTypes = new DmdType[DmdWellKnownTypeUtils.WellKnownTypesCount];
}

public DmdType GetWellKnownType(DmdWellKnownType wellKnownType, bool onlyCorLib) {
Expand Down

0 comments on commit bb19bf7

Please sign in to comment.