简体   繁体   English

Blazor 如何推断@typeparam 指令的数据类型?

[英]How Blazor infer the data type of @typeparam directive?

The code below was made using.Net 5.0.203下面的代码是使用.Net 5.0.203 制作的

I've this component我有这个组件

MyComponent.razor MyComponent.razor

@typeparam TItem

<p>MyComponent: @typeName</p>

@code {
    [Parameter]
    public IEnumerable<TItem> Data { get; set; }

    string typeName;

    protected override void OnInitialized()
    {
        typeName = typeof(TItem).Name;
    }
}

and here is the page calling the component这是调用组件的页面

Index.razor索引.razor

@page "/"

<MyComponent Data="@items"></MyComponent>

<p>Page: @itemsType</p>

@code
{
    string[] items = new string[2];

    string itemsType;

    protected override void OnInitialized()
    {
        itemsType = items.GetType().Name;
    }
}

I expected the type of TItem in MyComponent is String[] however it's String我预计MyComponentTItem的类型是String[]但它是String

How Blazor infer the type of TItem? Blazor如何推断TItem的类型?

When you use the generic constraint of typeparam TItem is says when you create the component you must specify what that type is.当您使用typeparam TItem的通用约束时,当您创建组件时,您必须指定该类型是什么。

When you called your component here:当你在这里调用你的组件时:

<MyComponent Data="@items"></MyComponent>

You passed the @items object as the parameter.您将@items object 作为参数传递。

If you don't explicitly tell the component what type it should expect, it will by default use whatever type is passed to it.如果你没有明确地告诉组件它应该期望什么类型,默认情况下它会使用传递给它的任何类型。

You passed it a string[] so it said "He gave me a string[] so i'm going to assume the TItem is of type string ".你给它传递了一个string[] ,所以它说“他给了我一个string[] ,所以我假设TItem的类型是string ”。 It does this because string[] is implicitly convertible to IEnumerable<TItem> since TItem can be any object unless you constrain that parameter.这样做是因为string[]可以隐式转换为IEnumerable<TItem>因为TItem可以是任何 object ,除非您限制该参数。 so it can only be certain types所以它只能是某些类型

Another thing of note is that TItem is the type of the object that is contained within the IEnumerable<TItem> parameter.另一件值得注意的事情是TItemIEnumerable<TItem>参数中包含的 object 的类型。 If you want to check to see what type of IEnumerable that was passed instead maybe do something like:如果您想查看传递的IEnumerable类型,则可以执行以下操作:

string[] s = new string[] { "fox" };

Check(s);

static void Check<T>(IEnumerable<T> maybeArray)
{
    Console.WriteLine(typeof(T)); // System.String
    Console.WriteLine(maybeArray is T[]); // true
    Console.WriteLine(maybeArray is List<T>); // false
}

If you want TItem to be a string[] consider instead accepting TItem as a parameter instead of IEnumerable<TItem> .如果您希望TItem成为string[] ,请考虑接受TItem作为参数而不是IEnumerable<TItem>

Such as:如:

[Parameter]
public TItem Data { get; set; }

Make sure to include a constraint on TItem if you need it to be enumerable.如果您需要它是可枚举的,请确保在TItem上包含一个约束。

For more information on how to include a generic constraint in blazor (it's weird) check out this post )有关如何在 blazor 中包含通用约束的更多信息(这很奇怪),请查看这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor 将数据@typeparam 传递给参数? - Blazor passing data @typeparam to parameter? 如何将变量传递到 blazor 中组件的 @typeparam - How to pass a variable into a component's @typeparam in blazor C# Blazor:如何在后面的代码中使用@typeparam? (有解决方法) - C# Blazor: How to use @typeparam in Code behind? (with workaround) 如何在 Blazor(子)组件中引用“@typeparam TModel”字段? - How can I reference a "@typeparam TModel" field in the Blazor (sub)-component? 如何评论一个动作的typeparam? - How to comment the typeparam of an Action? 剃刀组件中的“typeparam”语法错误 - blazor - 'typeparam' syntax error in razor components - blazor 以编程方式获取类型并将其用作typeparam - Get type programmatically and use it as a typeparam 如何在不知道typeparam类型的情况下检查对象是否从通用基类继承 - How to check if object inherits from a generic base class without knowing type of typeparam 如何推断 FHIR“选择”属性的正确数据类型 - How to infer the correct data type for a FHIR 'choice' property 将带有 typeparam 的复杂类型作为 CascadeParameter 传递给子 RenderFragment - Pass complex type with typeparam to child RenderFragment as CascadeParameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM