Created
August 29, 2022 14:33
-
-
Save mdimai666/2ff6afe64de414959f557fba6035a979 to your computer and use it in GitHub Desktop.
This file contains 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
//index.razor | |
@page "/" | |
@using HtmlAgilityPack | |
@using Microsoft.AspNetCore.Components.RenderTree | |
@using Microsoft.AspNetCore.Components.Rendering | |
@using System.Text.RegularExpressions | |
@using System.Reflection | |
@*<SurveyPrompt Title="How is Blazor working for you?" />*@ | |
<ErrorBoundary> | |
<ChildContent> | |
@my | |
</ChildContent> | |
<ErrorContent> | |
<p class="alert alert-danger "> | |
@context.Message | |
<pre>@context.StackTrace</pre> | |
</p> | |
</ErrorContent> | |
</ErrorBoundary> | |
<button @onclick=StateHasChanged>StateHasChanged</button> | |
@code { | |
[Inject] IJSRuntime js { get; set; } | |
static Dictionary<string, Type> dict = new(); | |
static RenderFragment frag => @<span>frag</span>; | |
protected override void OnInitialized() | |
{ | |
base.OnInitialized(); | |
dict = GetPages(typeof(Program)); | |
} | |
public static string html => @"<h1 x-target >Title</h1> | |
<div>simple div</div> | |
<_SurveyPrompt Title=""Work""/> | |
<div> | |
<div> | |
<Bashkirov klinch=""Kavabanga1"" Age=22 | |
Id=""4fe535d1-0917-4ea0-bbbf-6e8a4ebf0ebc"" >xxx</Bashkirov> | |
</div> | |
<div> | |
suuuu | |
</div> | |
</div> | |
<div>end</div>"; | |
RenderFragment my = builder => | |
{ | |
//builder.AddMarkupContent(0, "<h1>Hello, world2!</h1>\r\n\r\nWelcome to your new app.\r\n\r\n"); | |
//builder.OpenComponent<BlazorRenderHtmlTree.Shared.SurveyPrompt>(1); | |
//builder.AddAttribute(2, "Title", "How is Blazor working for you?"); | |
//builder.CloseComponent(); | |
var doc = new HtmlDocument(); | |
doc.LoadHtml(html); | |
RenderList(builder, doc.DocumentNode.ChildNodes); | |
}; | |
static object SetValue(PropertyInfo info, object value) | |
{ | |
var targetType = IsNullableType(info.PropertyType) | |
? Nullable.GetUnderlyingType(info.PropertyType) | |
: info.PropertyType; | |
var convertedValue = Convert.ChangeType(value, targetType); | |
//info.SetValue(instance, convertedValue, null); | |
return convertedValue; | |
} | |
public static bool IsNullableType(Type type) | |
{ | |
return type.IsGenericType | |
&& type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)); | |
} | |
static void RenderList(RenderTreeBuilder builder, HtmlNodeCollection ChildNodes) | |
{ | |
int index = 0; | |
foreach (var d in ChildNodes) | |
{ | |
if (d.NodeType == HtmlNodeType.Text) | |
{ | |
builder.AddContent(index, d.InnerText); | |
index++; | |
} | |
else if (d.Name.Length > 4 && dict.ContainsKey(d.OriginalName)) | |
{ | |
Type com = dict[d.OriginalName]; | |
builder.OpenComponent(index, com); | |
var props = com.GetProperties().ToDictionary(s => s.Name); | |
foreach (var a in d.Attributes) | |
{ | |
bool isComHasProp = props.ContainsKey(a.OriginalName); | |
if (isComHasProp) | |
{ | |
PropertyInfo prop = props[a.OriginalName]; | |
bool isStringProp = prop.PropertyType == typeof(string); | |
if (isStringProp) | |
{ | |
builder.AddAttribute(index, a.Name, a.Value); | |
} | |
else | |
{ | |
if (prop.PropertyType == typeof(Guid)) | |
{ | |
builder.AddAttribute(index, a.Name, Guid.Parse(a.Value)); | |
} | |
else | |
{ | |
//will cast | |
object castedValue = SetValue(prop, a.Value); | |
builder.AddAttribute(index, a.Name, castedValue); | |
} | |
} | |
} | |
else | |
{ | |
builder.AddAttribute(index, a.Name, a.Value); | |
} | |
index++; | |
} | |
if (d.HasChildNodes) | |
{ | |
RenderFragment ff = new RenderFragment(builder => | |
{ | |
//builder.AddContent(0, "xqxw"); | |
RenderList(builder, d.ChildNodes); | |
}); | |
builder.AddAttribute(index++, "ChildContent", ff); | |
} | |
builder.CloseComponent(); | |
} | |
else | |
{ | |
builder.OpenElement(index, d.OriginalName); | |
foreach (var a in d.Attributes) | |
{ | |
builder.AddAttribute(index, a.Name, a.Value); | |
} | |
if (d.HasChildNodes) | |
{ | |
RenderList(builder, d.ChildNodes); | |
} | |
else | |
{ | |
builder.AddMarkupContent(index, d.OuterHtml); | |
} | |
builder.CloseElement(); | |
index++; | |
} | |
index++; | |
} | |
} | |
async void TestXTarget() | |
{ | |
await js.InvokeVoidAsync("TestXTarget"); | |
} | |
public Dictionary<string, Type> GetPages(Type program) | |
{ | |
var type = typeof(ComponentBase); | |
var types = | |
//AppDomain.CurrentDomain.GetAssemblies() | |
//.SelectMany(s => s.GetTypes()) | |
//Assembly.GetAssembly(typeof(Program)).GetTypes() | |
Assembly.GetAssembly(program).GetTypes() | |
.Where(p => | |
type.IsAssignableFrom(p) | |
&& p.IsPublic | |
&& p.IsClass | |
&& !p.IsAbstract | |
); | |
Dictionary<string, Type> dict = new(); | |
string[] ignores = { "App", "_Imports" }; | |
foreach (var d in types) | |
{ | |
if (ignores.Contains(d.Name)) continue; | |
var attributes = d.GetCustomAttributes() ?? new List<Attribute>(); | |
bool isLayout = d.IsSubclassOf(typeof(LayoutComponentBase)); | |
bool isPage = attributes.Any(s => s is RouteAttribute); | |
if (!isLayout && !isPage) | |
{ | |
dict.Add(d.Name, d); | |
} | |
} | |
return dict; | |
} | |
} | |
//Bashkirov.razor | |
<section class=""> | |
<h1 class="fw-bold">Yeeeeeeeeeeeeeee: @Id</h1> | |
<div class="bg-info p-2">@Klinch</div> | |
<b>[ | |
@ChildContent | |
]</b> | |
<div class="text-danger">Age: @Age</div> | |
</section> | |
@code { | |
[Parameter] public string Klinch { get;set; } | |
[Parameter] public Guid Id { get;set; } | |
[Parameter] public int Age { get;set; } | |
[Parameter] public RenderFragment ChildContent { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment