Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/TypeGen/TypeGen.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using TypeGen.Cli.DependencyInjection;
using TypeGen.Core.Logging;
Expand Down
37 changes: 22 additions & 15 deletions src/TypeGen/TypeGen.Core/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static bool HasExportAttribute(this Type type, IMetadataReader reader)
{
Requires.NotNull(type, nameof(type));
Requires.NotNull(reader, nameof(reader));

return reader.GetAttribute<ExportTsClassAttribute>(type) != null ||
reader.GetAttribute<ExportTsInterfaceAttribute>(type) != null ||
reader.GetAttribute<ExportTsEnumAttribute>(type) != null;
Expand All @@ -39,7 +39,7 @@ public static IEnumerable<Type> GetExportMarkedTypes(this IEnumerable<Type> type
{
Requires.NotNull(types, nameof(types));
Requires.NotNull(reader, nameof(reader));

return types.Where(t => t.HasExportAttribute(reader));
}

Expand All @@ -53,7 +53,7 @@ public static IEnumerable<T> WithoutTsIgnore<T>(this IEnumerable<T> memberInfos,
{
Requires.NotNull(memberInfos, nameof(memberInfos));
Requires.NotNull(reader, nameof(reader));

return memberInfos.Where(i => reader.GetAttribute<TsIgnoreAttribute>(i) == null);
}

Expand Down Expand Up @@ -104,11 +104,18 @@ public static bool IsStatic(this MemberInfo memberInfo)
public static bool IsNullable(this MemberInfo memberInfo)
{
Requires.NotNull(memberInfo, nameof(memberInfo));

var contextualMember = memberInfo.ToContextualAccessor();
return contextualMember.Nullability == Nullability.Nullable;
try
{
var contextualMember = memberInfo.ToContextualAccessor();
return contextualMember.Nullability == Nullability.Nullable;
}
catch
{
// If the member is not a contextual member, we cannot determine nullability
return false;
}
}

/// <summary>
/// Checks if a property or field is nullable
/// </summary>
Expand All @@ -119,7 +126,7 @@ public static bool IsNullable(this Type type)
Requires.NotNull(type, nameof(type));
return Nullable.GetUnderlyingType(type) != null;
}

/// <summary>
/// Maps an enumerable to an enumerable of the elements' type names
/// </summary>
Expand All @@ -128,11 +135,11 @@ public static bool IsNullable(this Type type)
public static IEnumerable<string> GetTypeNames(this IEnumerable<object> enumerable)
{
Requires.NotNull(enumerable, nameof(enumerable));

return enumerable
.Select(c => c.GetType().Name);
}

/// <summary>
/// Shim for Type.GetInterface
/// </summary>
Expand All @@ -143,7 +150,7 @@ public static Type GetInterface(this Type type, string interfaceName)
{
Requires.NotNull(type, nameof(type));
Requires.NotNullOrEmpty(interfaceName, nameof(interfaceName));

return type.GetInterfaces()
.FirstOrDefault(i => i.Name == interfaceName || i.FullName == interfaceName);
}
Expand All @@ -164,17 +171,17 @@ public static IEnumerable<MemberInfo> GetTsExportableMembers(this Type type, IMe

if (!typeInfo.IsClass && !typeInfo.IsInterface && !typeInfo.IsStruct()) return Enumerable.Empty<MemberInfo>();

var fieldInfos = (IEnumerable<MemberInfo>) typeInfo.DeclaredFields
var fieldInfos = (IEnumerable<MemberInfo>)typeInfo.DeclaredFields
.WithMembersFilter();
var propertyInfos = (IEnumerable<MemberInfo>) typeInfo.DeclaredProperties

var propertyInfos = (IEnumerable<MemberInfo>)typeInfo.DeclaredProperties
.WithMembersFilter();

if (withoutTsIgnore)
{
fieldInfos = fieldInfos.WithoutTsIgnore(metadataReader);
propertyInfos = propertyInfos.WithoutTsIgnore(metadataReader);
}
}

return fieldInfos.Union(propertyInfos);
}
Expand Down
7 changes: 5 additions & 2 deletions src/TypeGen/TypeGen.Core/Generator/Services/TypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,11 @@ public bool TypeContainsBlacklistedType(Type type)
/// <exception cref="ArgumentNullException">Thrown when member or typeNameConverters is null</exception>
private string GetTsTypeNameForMember(MemberInfo memberInfo)
{
if (memberInfo.GetCustomAttribute<DynamicAttribute>() != null)
return "any";
try
{
if (memberInfo.GetCustomAttribute<DynamicAttribute>() != null)
return "any";
} catch { }

Type type = GetMemberType(memberInfo);
return GetTsTypeName(type, false);
Expand Down
13 changes: 10 additions & 3 deletions src/TypeGen/TypeGen.Core/Metadata/AttributeMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public TAttribute GetAttribute<TAttribute>(MemberInfo memberInfo) where TAttribu
Requires.NotNull(memberInfo, nameof(memberInfo));
return memberInfo.GetCustomAttribute<TAttribute>();
}

public IEnumerable<TAttribute> GetAttributes<TAttribute>(Type type) where TAttribute : Attribute
{
Requires.NotNull(type, nameof(type));
Expand All @@ -30,7 +30,7 @@ public IEnumerable<TAttribute> GetAttributes<TAttribute>(MemberInfo memberInfo)
Requires.NotNull(memberInfo, nameof(memberInfo));
return memberInfo.GetCustomAttributes(typeof(TAttribute), false) as TAttribute[];
}

public IEnumerable<object> GetAttributes(Type type)
{
Requires.NotNull(type, nameof(type));
Expand All @@ -40,7 +40,14 @@ public IEnumerable<object> GetAttributes(Type type)
public IEnumerable<object> GetAttributes(MemberInfo memberInfo)
{
Requires.NotNull(memberInfo, nameof(memberInfo));
return memberInfo.GetCustomAttributes(false);
try
{
return memberInfo.GetCustomAttributes(false);
}
catch (Exception ex)
{
return [];
}
}
}
}