Skip to content

Commit

Permalink
fix 2
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Aug 17, 2024
1 parent 9a5c466 commit 76078fc
Show file tree
Hide file tree
Showing 21 changed files with 452 additions and 558 deletions.
119 changes: 0 additions & 119 deletions src-blazor/WasmDynamicLinq/Pages/Home.razor.cs
Original file line number Diff line number Diff line change
@@ -1,126 +1,13 @@
using System.Linq.Dynamic.Core;
using System.Reflection;
using System.Reflection.Emit;

namespace WasmDynamicLinq.Pages;

public partial class Home
{
public class ExampleBase { }

private void Test()
{
// Define a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");

// Define a public class named "DynamicType"
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public);

// Define a public field of type string named "MyField"
typeBuilder.DefineField("MyField", typeof(string), FieldAttributes.Public);

//typeBuilder.DefineGenericParameters("MyField");

// Create the type
Type dynamicType = typeBuilder.CreateType();
//if (types.Length > 0)
{
//dynamicType = dynamicType.MakeGenericType(typeof(string));
}

// Create an instance of the dynamic type
dynamic dynamicObject = Activator.CreateInstance(dynamicType)!;

// Set the value of the field using reflection
FieldInfo fieldInfo = dynamicType.GetField("MyField", BindingFlags.Public | BindingFlags.Instance)!;
fieldInfo.SetValue(dynamicObject, "Hello, World!");

// Output the value
Console.WriteLine("Test Field Value: " + (string)fieldInfo.GetValue(dynamicObject));
Console.WriteLine("Test Field Value Dynamic: " + dynamicObject.MyField);
}

private void TestChatGPT()
{
// Define a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");

// Define a public generic class named "DynamicType`1" with one generic type parameter
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType`1", TypeAttributes.Public);

// Define a generic parameter "T"
GenericTypeParameterBuilder genericParameter = typeBuilder.DefineGenericParameters("T")[0];

// Define a public field of type T named "MyField"
FieldBuilder fieldBuilder = typeBuilder.DefineField("MyField", genericParameter, FieldAttributes.Public);

// Create the generic type
Type dynamicType = typeBuilder.CreateType();

// Make a closed generic type of "DynamicType<string>"
Type closedGenericType = dynamicType.MakeGenericType(typeof(string));

// Create an instance of the closed generic type
dynamic dynamicObject = Activator.CreateInstance(closedGenericType)!;

// Set the value of the field directly
FieldInfo fieldInfo = closedGenericType.GetField("MyField", BindingFlags.Public | BindingFlags.Instance)!;
fieldInfo.SetValue(dynamicObject, "Hello, World!");

// Get the value of the field directly
string fieldValue = (string)fieldInfo.GetValue(dynamicObject);

// Output the value
Console.WriteLine("Field Value: " + fieldValue);
Console.WriteLine("Test Field Value Dynamic: " + dynamicObject.MyField);
}

private void TestFail()
{
// Define a dynamic assembly and module
AssemblyName assemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("DynamicModule");

// Define a public class named "DynamicType"
TypeBuilder typeBuilder = moduleBuilder.DefineType("DynamicType", TypeAttributes.Public);

// Define a public field of type string named "MyField"
typeBuilder.DefineField("MyField", typeof(string), FieldAttributes.Public);

// Define Generic Parameter
typeBuilder.DefineGenericParameters("T0");

// Create the type
Type dynamicType = typeBuilder.CreateType();

// Make it generic
dynamicType = dynamicType.MakeGenericType(typeof(string));

// Create an instance of the dynamic type
dynamic dynamicObject = Activator.CreateInstance(dynamicType)!;

// Set the value of the field using reflection
FieldInfo fieldInfo = dynamicType.GetField("MyField", BindingFlags.Public | BindingFlags.Instance)!;
fieldInfo.SetValue(dynamicObject, "Hello, World!"); // this throws exception

// Output the value
Console.WriteLine("Test Field Value: " + (string)fieldInfo.GetValue(dynamicObject));
Console.WriteLine("Test Field Value Dynamic: " + dynamicObject.MyField);
}

protected override void OnInitialized()
{
base.OnInitialized();

Test();
//TestChatGPT();
//TestFail();

var o = new Order();
dynamic od = o;
Console.WriteLine(od.OrderId);
Expand Down Expand Up @@ -150,13 +37,8 @@ protected override void OnInitialized()
var fieldValue = f.GetValue(dynamicClass1);
Console.WriteLine($"Field {f.Name} Reflection Value = {fieldValue}");
Console.WriteLine(((dynamic)dynamicClass1).Name__Field);
break;
}





var orders = new List<Order>
{
new Order
Expand Down Expand Up @@ -194,7 +76,6 @@ protected override void OnInitialized()

Console.WriteLine("DynamicClass = " + element["Id"]);

// Fail
Console.WriteLine("dynamic = " + ((dynamic)element).Id);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_0)
#if !(NETSTANDARD2_0)
[assembly: Guid("b467c675-c014-4be3-85b9-9578941d28c8")]
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ private static Expression OptimizeExpression(Expression expression)
{
var optimized = ExtensibilityPoint.QueryOptimizer(expression);

#if !(WINDOWS_APP45x || SILVERLIGHT)
#if !(SILVERLIGHT)
if (optimized != expression)
{
TraceSource.TraceEvent(TraceEventType.Verbose, 0, "Expression before : {0}", expression);
Expand All @@ -1116,4 +1116,4 @@ private static Expression OptimizeExpression(Expression expression)
}
#endregion Private Helpers
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_0)
#if !(NETSTANDARD2_0)
[assembly: Guid("b467c675-c014-4be3-85b9-9578941d28c8")]
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_1)
#if !(NETSTANDARD2_1)
[assembly: Guid("b467c675-c014-4b55-85b9-9578941d28c8")]
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_1)
#if !(NETSTANDARD2_1)
[assembly: Guid("b467c675-c014-4b55-85b9-9578941d28c8")]
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_1)
#if !(NETSTANDARD2_1)
[assembly: Guid("b467c675-c014-4b55-85b9-9578941d2ef7")]
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.InteropServices;

[assembly: ComVisible(false)]
#if !(WINDOWS_APP || NETSTANDARD2_1)
#if !(NETSTANDARD2_1)
[assembly: Guid("b467c675-c014-4b55-85b9-9578941d2ef7")]
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected IEnumerable<Type> FindTypesMarkedWithDynamicLinqTypeAttribute(IEnumera
return null;
}

#if (WINDOWS_APP || UAP10_0 || NETSTANDARD)
#if (UAP10_0 || NETSTANDARD)
/// <summary>
/// Gets the assembly types annotated with <see cref="DynamicLinqTypeAttribute"/> in an Exception friendly way.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/System.Linq.Dynamic.Core/DefaultAssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class DefaultAssemblyHelper(ParsingConfig parsingConfig) : IAssemblyHel

public Assembly[] GetAssemblies()
{
#if WINDOWS_APP || UAP10_0 || NETSTANDARD || WPSL
#if UAP10_0 || NETSTANDARD || WPSL
throw new NotSupportedException();
#elif NET35
return AppDomain.CurrentDomain.GetAssemblies();
Expand Down
Loading

0 comments on commit 76078fc

Please sign in to comment.