Skip to content

Commit

Permalink
Type CreateInstance extension
Browse files Browse the repository at this point in the history
  • Loading branch information
oleghcp committed Aug 27, 2024
1 parent 875cd1f commit e652de8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Code/Runtime/OlegHcp/CSharp/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public static object GetDefaultValue(this Type self)
return Helper.GetDefaultValue(self);
}

public static object CreateInstance(this Type self, bool allowNonPublicConstructor = false)
{
return Activator.CreateInstance(self, allowNonPublicConstructor);
}

public static object CreateInstance(this Type self, params object[] args)
{
return Activator.CreateInstance(self, args);
}

public static bool IsAssignableTo(this Type self, Type targetType)
{
if (targetType == null)
Expand Down
6 changes: 4 additions & 2 deletions Code/Runtime/OlegHcp/Tools/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public static void Swap<T>(ref T a, ref T b)

public static object GetDefaultValue(Type type)
{
switch (type.GetTypeCode())
TypeCode typeCode = type.GetTypeCode();

switch (typeCode)
{
case TypeCode.Boolean: return default(bool);
case TypeCode.Byte: return default(byte);
Expand All @@ -42,7 +44,7 @@ public static object GetDefaultValue(Type type)
case TypeCode.String:
return null;

default: throw new UnsupportedValueException(type.GetTypeCode());
default: throw new UnsupportedValueException(typeCode);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Oleg Pulkin",
"email": "oleghcp@gmail.com"
},
"version": "2.3.7",
"version": "2.3.8",
"unity": "2019.4",
"description": "A set of helpful code stuff for Unity.",
"keywords": [ "oleghcp", "oleg", "hcp", "unitytools", "tools" ],
Expand Down

0 comments on commit e652de8

Please sign in to comment.