Created
March 29, 2020 20:50
-
-
Save StefH/6229f0f264aeb68b567e5bf90b960922 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq.Expressions; | |
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
using AngleSharpWrappers; | |
using Bunit; | |
using Microsoft.AspNetCore.Components; | |
namespace ConsoleAppBUnit | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var x = new X(); | |
var r = x.RenderComponent<Com>(ComponentParameter.CreateParameter("Age", 42)); | |
var cut = x.RenderComponent<Com>(c => c.Name == "x", c => c.Age == 42); | |
} | |
class X : TestComponentBase | |
{ | |
public IRenderedComponent<TComponent> RenderComponent<TComponent>(params Expression<Func<TComponent, object>>[] parameters) where TComponent : class, IComponent | |
{ | |
var componentParameters = new List<ComponentParameter>(); | |
foreach (var parameter in parameters) | |
{ | |
if (parameter.Body is UnaryExpression body) | |
{ | |
if (body.Operand is BinaryExpression methodBinaryExpression) | |
{ | |
if (methodBinaryExpression.Left is MemberExpression left && methodBinaryExpression.Right is ConstantExpression right) | |
{ | |
var name = left.Member.Name; | |
var value = right.Value; | |
componentParameters.Add(ComponentParameter.CreateParameter(name, value)); | |
} | |
} | |
} | |
} | |
return base.RenderComponent<TComponent>(componentParameters.ToArray()); | |
} | |
} | |
class Com : IComponent | |
{ | |
[Parameter] | |
public string Name { get; set; } | |
[Parameter] | |
public int Age { get; set; } | |
public void Attach(RenderHandle renderHandle) | |
{ | |
//throw new NotImplementedException(); | |
} | |
public Task SetParametersAsync(ParameterView parameters) | |
{ | |
return Task.CompletedTask; // throw new NotImplementedException(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment