Skip to content

Commit

Permalink
fix: Error in copy and paste menu when right clicking on a type witho…
Browse files Browse the repository at this point in the history
…ut parameterless constructor
  • Loading branch information
fuqunaga committed Aug 28, 2024
1 parent 350a433 commit dec40c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Pool;

Expand Down Expand Up @@ -291,8 +292,11 @@ private static bool DictionaryToObject_Generic(Type type, IEnumerable<Dictionary

// 一つも一致する値がないと場合は失敗
var hasValue = false;

var obj = TypeUtility.HasParameterlessConstructor(type)
? Activator.CreateInstance(type)
: RuntimeHelpers.GetUninitializedObject(type);

var obj = Activator.CreateInstance(type);
foreach (var child in children)
{
if (!child.TryGetValue(EditorFormatKey.Name, out var nameObject))
Expand Down
12 changes: 12 additions & 0 deletions Packages/ga.fuquna.rosettaui/Runtime/Utilities/TypeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private static ReflectionCache.MemberData GetMemberData(Type type, string proper
}


public static bool HasParameterlessConstructor(Type type)
{
return GetReflectionCache(type).HasParameterlessConstructor;
}

public static Type GetPropertyOrFieldType(Type type, string propertyOrFieldName)
{
return GetMemberData(type, propertyOrFieldName).type;
Expand Down Expand Up @@ -203,8 +208,15 @@ private class ReflectionCache
public readonly Dictionary<string, MemberData> memberDataTable;
public readonly Dictionary<string, Type> uiTargetPropertyOrFieldsNameTypeDic;

private readonly Type _type;
private bool? _hasParameterlessConstructor;

public bool HasParameterlessConstructor => _hasParameterlessConstructor ??= _type.IsValueType || _type.GetConstructor(Type.EmptyTypes) != null;

public ReflectionCache(Type type, IReadOnlyCollection<FieldInfo> fis, IEnumerable<PropertyInfo> pis)
{
_type = type;

memberDataTable = fis.Select(fi => (fi as MemberInfo, fi.FieldType))
.Concat(pis.Select(pi => (pi as MemberInfo, pi.PropertyType)))
.ToDictionary(
Expand Down

0 comments on commit dec40c4

Please sign in to comment.