Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/microsoft/fluentui-blazor in…
Browse files Browse the repository at this point in the history
…to dev
  • Loading branch information
vnbaaij committed Jul 16, 2024
2 parents 8b8e489 + adf2b77 commit 5044ae9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/Demo/Shared/Components/ApiDocumentation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
{
var id = Identifier.NewId();
<FluentStack Orientation="Orientation.Horizontal" HorizontalGap="0">
<div>Preview:</div>
<div>Preview:&nbsp;</div>
<FluentIcon Id="@id" Value="@context.Icon" Width="20px" />
<FluentTooltip Anchor="@id" Position="TooltipPosition.Right">
@context.Default
Expand Down
56 changes: 32 additions & 24 deletions examples/Demo/Shared/Components/ApiDocumentation.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ private class MemberDescription
public MemberTypes MemberType { get; set; } = MemberTypes.Property;
public string Name { get; set; } = "";
public string Type { get; set; } = "";
public string[] EnumValues { get; set; } = Array.Empty<string>();
public string[] Parameters { get; set; } = Array.Empty<string>();
public string[] EnumValues { get; set; } = [];
public string[] Parameters { get; set; } = [];
public string? Default { get; set; } = null;
public string Description { get; set; } = "";
public bool IsParameter { get; set; }
Expand All @@ -39,7 +39,7 @@ private class MemberDescription
/// Default for this parameter is 'typeof(string)'
/// </summary>
[Parameter]
public Type[] InstanceTypes { get; set; } = new[] { typeof(string) };
public Type[] InstanceTypes { get; set; } = [typeof(string)];

/// <summary>
/// Gets or sets the label used for displaying the type parameter.
Expand Down Expand Up @@ -70,29 +70,37 @@ private IEnumerable<MemberDescription> GetMembers(MemberTypes type)
{
List<MemberDescription>? members = [];

object? obj;
if (Component.IsGenericType)
object? obj = null;
var created = false;

object? GetObjectValue(string propertyName)
{
if (InstanceTypes is null)
if (!created)
{
throw new ArgumentNullException(nameof(InstanceTypes), "InstanceTypes must be specified when Component is a generic type");
}
if (Component.IsGenericType)
{
if (InstanceTypes is null)
{
throw new ArgumentNullException(nameof(InstanceTypes), "InstanceTypes must be specified when Component is a generic type");
}

// Supply the type to create the generic instance with (needs to be an array)
Type[] typeArgs = InstanceTypes;
Type constructed = Component.MakeGenericType(typeArgs);
// Supply the type to create the generic instance with (needs to be an array)
obj = Activator.CreateInstance(Component.MakeGenericType(InstanceTypes));
}
else
{
obj = Activator.CreateInstance(Component);
}
created = true;
}

obj = Activator.CreateInstance(constructed);
}
else
{
obj = Activator.CreateInstance(Component);
}
return obj?.GetType().GetProperty(propertyName)?.GetValue(obj);
};

IEnumerable<MemberInfo>? allProperties = Component.GetProperties().Select(i => (MemberInfo)i);
IEnumerable<MemberInfo>? allMethods = Component.GetMethods().Where(i => !i.IsSpecialName).Select(i => (MemberInfo)i);
var allProperties = Component.GetProperties().Select(i => (MemberInfo)i);
var allMethods = Component.GetMethods().Where(i => !i.IsSpecialName).Select(i => (MemberInfo)i);

foreach (MemberInfo memberInfo in allProperties.Union(allMethods).OrderBy(m => m.Name))
foreach (var memberInfo in allProperties.Union(allMethods).OrderBy(m => m.Name))
{
try
{
Expand All @@ -105,7 +113,7 @@ private IEnumerable<MemberDescription> GetMembers(MemberTypes type)
{
var isParameter = memberInfo.GetCustomAttribute<ParameterAttribute>() != null;

Type t = propertyInfo.PropertyType;
var t = propertyInfo.PropertyType;
var isEvent = t == typeof(EventCallback) || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(EventCallback<>));

// Parameters/properties
Expand All @@ -115,11 +123,11 @@ private IEnumerable<MemberDescription> GetMembers(MemberTypes type)
var defaultVaue = "";
if (propertyInfo.PropertyType.IsValueType || propertyInfo.PropertyType == typeof(string))
{
defaultVaue = obj?.GetType().GetProperty(propertyInfo.Name)?.GetValue(obj)?.ToString();
defaultVaue = GetObjectValue(propertyInfo.Name)?.ToString();
}
else if (propertyInfo.PropertyType == typeof(Icon))
{
if (obj?.GetType().GetProperty(propertyInfo.Name)?.GetValue(obj) is Icon value)
if (GetObjectValue(propertyInfo.Name) is Icon value)
{
icon = value;
defaultVaue = $"{value.Variant}.{value.Size}.{value.Name}";
Expand Down Expand Up @@ -201,6 +209,6 @@ private static string[] GetEnumValues(PropertyInfo? propertyInfo)
}
}

return Array.Empty<string>();
return [];
}
}

0 comments on commit 5044ae9

Please sign in to comment.