aspnetcore/test/Microsoft.AspNetCore.Blazor.../Razor
Ryan Nowak 100382bf71 Add Type Inference for Generic-Typed Components
This change allows you to use generic-typed components without
explicitly specify type arguments.

**Before:**
```
<Grid Items="@MyItems" TItem="Person">
...
</Grid>
```

**After:**
```
<Grid Items="@MyItems">
...
</Grid>
```

Where possible, the type of the component will be inferred based on the
values passed to component parameters. This won't work like magic, you
have to specify parameters that provide type arguments for all of the
component's type parameters.

This is a best effort system, and it's largely based on the limitations
and behaviours of generic type inference in C#. We think it will work
well with most Blazor features and for most reasonable components. You
should not forget it's easy to specify type arguments, because you may
still have to in some cases.

In particular you may notice issues if you are trying to use a generic
typed component where all of the parameters are delegates or templates.
Type inference for delegates/lambdas in C# is based on the context. Any
time you combine generics and delegates it's easy to get into a scenario
where the compiler won't infer the correct type, or will give up.

----
The type inference feature works by generating a 'thunk' method in
*caller* that can act as a site for type inference (C# does not support
inference on constructors).

For our grid example above, the non-inferenced code will look something
like:
```
builder.OpenComponent<Grid<Person>>(0);
builder.AddAttribute(1, "Items", MyItems);
builder.CloseComponent();
```

Note that we can only write the type `Grid<Person>` because you wrote
`Person` in your code. What you type in the `TItem` attribute value gets
inserted into the generated code such that it fills in the generic type
parameter.

On the other hand, if you want is to infer the type, we have to do some
compiler magic. That looks like:
```
__Blazor.(long generated name).TypeInference.CreateGrid_0();
...

// elsewhere in the file
internal static class TypeInference
{
    public static void CreateGrid_0<TItem>(RenderTreeBuilder builder,
    int seq, int __seq0, global::System.Collections.Generic.List<TItem>
    __arg0)
        {
	        builder.OpenComponent<Grid<TItem>>(seq);
		        builder.AddAttribute(__seq0, "Items", __arg0);
			        builder.CloseComponent();
				    }
				    }
				    ```

				    This allows us to rely on the C#
				    compiler for itype inference.
2018-09-20 12:19:26 -07:00
..
InitializeTestFileAttribute.cs Add Support for Templated Components (#1404) 2018-09-10 18:59:51 -07:00
IntermediateNodeSerializer.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
IntermediateNodeVerifier.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
IntermediateNodeWriter.cs Add Type Inference for Generic-Typed Components 2018-09-20 12:19:26 -07:00
NotFoundProjectItem.cs
RazorDiagnosticSerializer.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
SourceMappingsSerializer.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
TestFile.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
TestProject.cs Add Razor baseline test infrastructure 2018-04-04 08:05:59 -07:00
VirtualProjectFileSystem.cs
VirtualProjectItem.cs