-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [TypeWrapping]型の読み替え処理をTypeConverterとして1クラスに分離
今後の機能拡充に際して、同様の処理が様々な箇所で必要となるため
- Loading branch information
1 parent
ce85a40
commit 88a5692
Showing
5 changed files
with
104 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using UnembeddedResources; | ||
|
||
namespace TypeWrapping | ||
{ | ||
public class TypeConverter | ||
{ | ||
private class ResourceSet | ||
{ | ||
private readonly ResourceLocalizer Localizer = ResourceLocalizer.FromResXOfType<TypeConverter>("TypeWrapping"); | ||
|
||
[ResourceStringHolder(nameof(Localizer))] public Resource<string> ElementTypesButArrayNotSupported { get; private set; } | ||
|
||
public ResourceSet() | ||
{ | ||
ResourceLoader.LoadAndSetAll(this); | ||
} | ||
} | ||
|
||
private static readonly Lazy<ResourceSet> Resources = new Lazy<ResourceSet>(); | ||
|
||
static TypeConverter() | ||
{ | ||
#if DEBUG | ||
_ = Resources.Value; | ||
#endif | ||
} | ||
|
||
private readonly Dictionary<Type, Type> ConvertibleTypes; | ||
private readonly Dictionary<Type, Type> TypeBridges; | ||
|
||
public TypeConverter(Dictionary<Type, Type> convertibleTypes, Dictionary<Type, Type> typeBridges = null) | ||
{ | ||
ConvertibleTypes = convertibleTypes; | ||
TypeBridges = typeBridges ?? new Dictionary<Type, Type>(); | ||
} | ||
|
||
public Type Convert(Type source) | ||
{ | ||
if (source.IsGenericParameter) | ||
{ | ||
Type declaringSource = source.DeclaringType; | ||
Type declaringOriginal = Convert(declaringSource); | ||
|
||
return (declaringOriginal as TypeInfo).GenericTypeParameters[source.GenericParameterPosition]; | ||
} | ||
else if (source.IsConstructedGenericType) | ||
{ | ||
Type sourceParent = source.GetGenericTypeDefinition(); | ||
Type originalParent = ParseSimpleType(sourceParent); | ||
|
||
Type[] sourceChildren = source.GetGenericArguments(); | ||
Type[] originalChildren = new Type[sourceChildren.Length]; | ||
for (int i = 0; i < sourceChildren.Length; i++) | ||
{ | ||
originalChildren[i] = ParseSimpleType(sourceChildren[i]); | ||
} | ||
|
||
Type result = originalParent.MakeGenericType(originalChildren); | ||
return result; | ||
} | ||
else if (source.HasElementType) | ||
{ | ||
if (!source.IsArray || source.Name.Contains("*")) throw new NotSupportedException(Resources.Value.ElementTypesButArrayNotSupported.Value); | ||
|
||
Type sourceElement = source.GetElementType(); | ||
Type originalElement = ParseSimpleType(sourceElement); | ||
|
||
int rank = source.GetArrayRank(); | ||
Type result = rank == 1 ? originalElement.MakeArrayType() : originalElement.MakeArrayType(rank); | ||
return result; | ||
} | ||
else | ||
{ | ||
return ParseSimpleType(source); | ||
} | ||
|
||
|
||
Type ParseSimpleType(Type simpleSource, bool allowBridgedTypes = true) | ||
{ | ||
if (!ConvertibleTypes.TryGetValue(simpleSource, out Type result)) | ||
{ | ||
if (allowBridgedTypes && TypeBridges.TryGetValue(simpleSource, out Type bridgedTo)) | ||
{ | ||
return ParseSimpleType(bridgedTo, false); | ||
} | ||
} | ||
|
||
return result ?? simpleSource; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters