From 58b0ad3493c208acb87794f14275b86d2ccac122 Mon Sep 17 00:00:00 2001 From: Daniel Cronqvist Date: Sat, 7 Sep 2024 13:03:27 +0200 Subject: [PATCH] Make use of generic type parameter in generic methods --- .../CustomTypes/CustomEnumDefinitionTests.cs | 0 .../Properties/CustomTypes/CustomClassDefinition.cs | 12 ++++++++++-- .../Properties/CustomTypes/CustomEnumDefinition.cs | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 src/DotTiled.Tests/Properties/CustomTypes/CustomEnumDefinitionTests.cs diff --git a/src/DotTiled.Tests/Properties/CustomTypes/CustomEnumDefinitionTests.cs b/src/DotTiled.Tests/Properties/CustomTypes/CustomEnumDefinitionTests.cs new file mode 100644 index 0000000..e69de29 diff --git a/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs b/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs index 3e65094..83e0fbe 100644 --- a/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs +++ b/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs @@ -111,7 +111,15 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition if (type == typeof(string) || !type.IsClass) throw new ArgumentException("Type must be a class.", nameof(type)); - return FromClass(() => Activator.CreateInstance(type)); + var instance = Activator.CreateInstance(type); + var properties = type.GetProperties(); + + return new CustomClassDefinition + { + Name = type.Name, + UseAs = CustomClassUseAs.All, + Members = properties.Select(p => ConvertPropertyInfoToIProperty(instance, p)).ToList() + }; } /// @@ -130,7 +138,7 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition public static CustomClassDefinition FromClass(Func factory) where T : class { var instance = factory(); - var type = instance.GetType(); + var type = typeof(T); var properties = type.GetProperties(); return new CustomClassDefinition diff --git a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs index e155b38..719f1ef 100644 --- a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs +++ b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace DotTiled; @@ -44,4 +45,9 @@ public class CustomEnumDefinition : ICustomTypeDefinition /// Whether the value should be treated as flags. /// public bool ValueAsFlags { get; set; } + + // public CustomEnumDefinition FromEnum(Type enumType) + // { + // if (!enumType.Is) + // } }