Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public CSharpType(Type type, IReadOnlyList<CSharpType> arguments, bool isNullabl
Name = type.IsGenericType ? type.Name.Substring(0, type.Name.IndexOf('`')) : type.Name;
IsValueType = type.IsValueType;
Namespace = type.Namespace ?? string.Empty;
FullyQualifiedName = $"{Namespace}.{Name}";
IsPublic = type.IsPublic && arguments.All(t => t.IsPublic);
// open generic parameter such as the `T` in `List<T>` is considered as declared inside the `List<T>` type as well, but we just want this to be the pure nested type, therefore here we exclude the open generic parameter scenario
// for a closed generic parameter such as the `string` in `List<string>`, it is just an ordinary type without a `DeclaringType`.
Expand All @@ -132,27 +131,6 @@ private static void ValidateArguments(Type type, IReadOnlyList<CSharpType> argum
}
}

internal CSharpType(
TypeProvider implementation,
string providerNamespace,
IReadOnlyList<CSharpType> arguments,
CSharpType? baseType)
: this(
implementation.Name,
providerNamespace,
implementation is EnumProvider ||
implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct) ||
implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Enum),
false,
implementation.DeclaringTypeProvider?.Type,
arguments,
implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public) && arguments.All(t => t.IsPublic),
implementation.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct),
baseType,
implementation.IsEnum? implementation.EnumUnderlyingType.FrameworkType : null)
{
}

internal CSharpType(
string name,
string ns,
Expand All @@ -174,17 +152,20 @@ internal CSharpType(
IsValueType = isValueType;
IsNullable = isNullable;
Namespace = ns;
FullyQualifiedName = $"{ns}.{name}";
DeclaringType = declaringType;
IsPublic = isPublic;
IsStruct = isStruct;
BaseType = baseType;
_underlyingType = underlyingEnumType;
}

public string Namespace { get; set; }
public string Name { get; private init; }
internal string FullyQualifiedName { get; private init; }
public string Namespace { get; private set; }

/// <summary>
/// Gets or sets the name of the type.
/// </summary>
public string Name { get; private set; }
internal string FullyQualifiedName => $"{Namespace}.{Name}";
public CSharpType? DeclaringType { get; private init; }
public bool IsValueType { get; private init; }
public bool IsEnum => _underlyingType is not null;
Expand Down Expand Up @@ -666,5 +647,22 @@ private CSharpType GetRootType()

return returnType;
}

/// <summary>
/// Update the instance with given parameters.
/// </summary>
/// <param name="name">Name of the <see cref="CSharpType"/></param>
/// <param name="namespace">Namespace of the <see cref="CSharpType"/></param>
public void Update(string? name = null, string? @namespace = null)
{
if (name != null)
{
Name = name;
}
if (@namespace != null)
{
Namespace = @namespace;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ private IReadOnlyList<FieldProvider> BuildAllCustomFields()

private string? _relativeFilePath;

public string Name => _name ??= CustomCodeView?.Name ?? BuildName();

private string? _name;
public string Name => Type.Name;

protected virtual FormattableString Description { get; } = FormattableStringHelpers.Empty;

Expand All @@ -106,12 +104,23 @@ public string? Deprecated
private set => _deprecated = value;
}

private string? _name;
private CSharpType? _type;
public CSharpType Type => _type ??= new(
this,
CustomCodeView?.BuildNamespace() ?? BuildNamespace(),
GetTypeArguments(),
GetBaseType());
private CSharpType[]? _arguments;
public CSharpType Type => _type ??=
new(
_name ??= CustomCodeView?.Name ?? BuildName(),
CustomCodeView?.BuildNamespace() ?? BuildNamespace(),
this is EnumProvider ||
DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct) ||
DeclarationModifiers.HasFlag(TypeSignatureModifiers.Enum),
false,
DeclaringTypeProvider?.Type,
_arguments ??= GetTypeArguments(),
DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public) && _arguments.All(t => t.IsPublic),
DeclarationModifiers.HasFlag(TypeSignatureModifiers.Struct),
GetBaseType(),
IsEnum ? EnumUnderlyingType.FrameworkType : null);

protected virtual bool GetIsEnum() => false;
public bool IsEnum => GetIsEnum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public void FullyQualifiedNamePopulatedForFrameworkTypes(Type type)
public void FullyQualifiedNamePopulatedForTypeProviders()
{
var provider = new TestTypeProvider();
var type = new CSharpType(provider, "TestNamespace", [], null);
Assert.AreEqual("TestNamespace.TestName", type.FullyQualifiedName);
Assert.AreEqual("Test.TestName", provider.Type.FullyQualifiedName);
}

[TestCase(typeof(int))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ public async Task CanRemoveConstructors()
{
// Parameter type doesn't match
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[new ParameterProvider("param1", $"", typeof(bool))]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[
Expand All @@ -203,15 +203,15 @@ [new ParameterProvider("param1", $"", typeof(bool))]),
]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[
new ParameterProvider("param1", $"", new FooTypeProvider("Sample").Type)
]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[
Expand Down Expand Up @@ -240,19 +240,19 @@ public async Task DoesNotRemoveConstructorsThatDoNotMatch()
var constructors = new[]
{
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[new ParameterProvider("param1", $"", typeof(bool))]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[
Expand All @@ -261,7 +261,7 @@ [new ParameterProvider("param1", $"", typeof(bool))]),
]),
Snippet.ThrowExpression(Snippet.Null), client),
new ConstructorProvider(new ConstructorSignature(
new CSharpType(client, "Samples", [typeof(int)], null),
client.Type,
$"",
MethodSignatureModifiers.Public,
[
Expand Down
Loading