Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Add IsAnonymous for Types
  • Loading branch information
uurha authored Feb 29, 2024
2 parents c108bed + f955839 commit a46715c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Assets/BetterExtensions/Runtime/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityObject = UnityEngine.Object;

namespace Better.Extensions.Runtime
Expand Down Expand Up @@ -136,6 +137,24 @@ public static IEnumerable<Type> GetAllInheritedTypesWithoutUnityObject(this Type
return self.GetAllInheritedTypes(unityObjectType);
}

public static bool IsAnonymous(this Type type)
{
if (type.IsGenericType)
{
var d = type.GetGenericTypeDefinition();
if (d.IsClass && d.IsSealed && d.Attributes.HasFlag(TypeAttributes.NotPublic))
{
var attributes = d.GetCustomAttribute<CompilerGeneratedAttribute>(false);
if (attributes != null)
{
return true;
}
}
}

return false;
}

public static IEnumerable<Type> GetAllInheritedTypesOfRawGeneric(this Type self)
{
if (self == null)
Expand Down
5 changes: 5 additions & 0 deletions Assets/BetterExtensions/Runtime/Utility/TypeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public static bool IsNullable<T>()
var type = typeof(T);
return type.IsNullable();
}

public static bool IsAnonymous<T>()
{
return typeof(T).IsAnonymous();
}
}
}
2 changes: 1 addition & 1 deletion Assets/BetterExtensions/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.uurha.betterextensions",
"displayName": "Better Extensions",
"version": "1.5.3",
"version": "1.5.4",
"unity": "2020.1",
"description": "Unity extensions, serialize extension, async extension, string extension and UI extensions",
"dependencies": {
Expand Down

0 comments on commit a46715c

Please sign in to comment.