754
questions
0
votes
0
answers
47
views
Is it possible to execute IL instructions one by one using System.Reflection.Emit?
In the preceding C# code, I'm creating a dynamic method and adding a few IL instructions to it, and then invoking the dynamic method:
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new(&...
4
votes
1
answer
66
views
Unable to implement INotifyPropertyChanged with System.Reflection.Emit
I'm trying to implement a dynamic class that can be bound to an Avalonia DataGrid. This class should implement INotifyPropertyChanged in order to use DataGrid edition. After search, it seems the best ...
0
votes
0
answers
35
views
Replacing a method call via reflection in C# does not work in a Unity environment
In Unity6 (6000.0.7f1), I've tried to redirect a method call to my own method using the MethodRedirect extension method from this GitHub repo.
From the Scenario1 example code in the GitHub repo, I ...
0
votes
0
answers
78
views
Trying to do struct delegate optimization with runtime generated struct C#
I do a lot of hashing in hot path; these hashing function looks something like this: f(input) mod size. I do not know size beforehand.
When tested, I got a significant speedup from compiler ...
0
votes
0
answers
45
views
using reflection to assign a dynamically created property
I am looking to assign a dynamically created property value but apparently I only get the value assigned to the backing field and not the actual property value when I call to propertyInfo.SetValue(...
2
votes
2
answers
59
views
Why is this Reflection.Emit code causing an AccessViolationException?
As a general rule, managed code is not supposed to be able to cause access violations. That happens in native code or in unsafe code. But somehow, with a bit of playing around with some Reflection....
1
vote
1
answer
61
views
How to use a generic type with a runtime-defined type parameter in Reflection.Emit
Running this under .NET 8.
IJoinable<T1, T2> already exists in the codebase as a generic interface. It declares one method, T2 Join(T1 value). The types being used for the type parameters (...
1
vote
2
answers
112
views
Use c# reflection to inline method calls "manually"
I'm running math heavy code on GPU with ILGPU, I organized it in modular classes segregated by interfaces to implement different algorithms that are modular.
Now ILGPU allows only to run static ...
0
votes
1
answer
44
views
C# Reflection emit invalid program when calling emited method from generic type that implements interface
I am generating a proxy dynamically in C# to wrap over a derived type from an interface, intercepting the methods from the interface and calling another method with the intercepted parameters.
My ...
0
votes
0
answers
35
views
C# Reflection Emit Invalid program exception when implementing generic interface
I am implementing a dynamic proxy class that implements an interface and exposes the methods inside that interface giving the user the option to call the method on the original object.
The proxy class ...
1
vote
1
answer
44
views
What is wrong with this Reflection.Emit for value conversion delegates?
Sorry for this long question but I feel I have to provide a bit more background as my issue is very specific.
Bigger picture
I am developing on a Unity tool to be used specifically for Embedded Linux ...
0
votes
1
answer
97
views
.NET reflection emit - what am I doing wrong in writing this method in MSIL?
I am trying to dynamically create a delegate that returns a list of the values of all properties defined on an object, using reflection emit in C#.
I have started with the examples given here:
https://...
0
votes
0
answers
103
views
Generating Complex Objects with Dynamic Properties Using Reflection Emit at Runtime
So The concept in my head kind of straightforward I'm just unsure how to implement this correctly.
I've got a class called Banana and a class Called Dog for simplicity sake.
Banana has a Dictionary of ...
0
votes
1
answer
46
views
How to stop Reflection.Emit from creating a console window?
I am generating an executable with System.Reflection.Emit (in .NET Framework, so I can save it to disk) and I want the generated exectuable to not have a console window (it should just perform some ...
1
vote
1
answer
80
views
Undefined behavior when trying to Memory.Slice using il.Emit
I am trying to write a dynamic method that takes ReadOnlyMemory<byte> and processes it. But I have experienced very weird behavior when trying to slice the memory. It seems like the arguments to ...
0
votes
0
answers
22
views
How to embed .manifest file using Reflection.Emit?
I want to embed a .manifest file inside my generated assembly (I am using .NET Framework 4.8.1).
There is a method called DefineManifestResource on ModuleBuilder, but I'm not sure if it is the right ...
2
votes
1
answer
56
views
Emitted properties do not appear in the debugger
I use System.Reflection.Emit to create classes at runtime that implement various interfaces which contain methods and properties.
When generating a class, I generate one concrete method for each ...
0
votes
0
answers
685
views
System.InvalidProgramException Common Language Runtime detected an invalid program for Reflection
System.InvalidProgramException : Common Language Runtime detected an invalid program.
Seeing this error message whilst trying to check the expression's correctness and if it is the correct IL.
The ...
1
vote
0
answers
229
views
How to call .NET Framework methods in their own process from .NET 7?
I'm working on a .NET 7 WPF application that relies on a .NET Framework (4.8.1) library that uses Reflection.Emit (with AssemblyBuilder.Save). When I call methods from that library from my application,...
1
vote
1
answer
507
views
How can I create dynamic assemblies and save them to disk using .NET 5 or later?
I am trying to use AssemblyBuilder to create dynamic assemblies and save them in .NET 5,6,7
// Create the dynamic assembly
var builder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("...
-2
votes
1
answer
80
views
Is it possible to have implicit conversion operators being called during casts made by a method generated dynamically ? (eg: IL emit)
I have have the following classes A and B:
public class A
{
}
public class B
{
public static implicit operator A(B value)
{
return new A();
}
}
As expected, the following ...
0
votes
1
answer
57
views
Is it safe to use Dynamic class creation at runtime? Apple will allow to publish it in appstore?
We did Dynamic Class Creation at runtime using reflection and Activator.CreateInstance() method.
We followed the below link for VM part creation
https://devblogs.microsoft.com/xamarin/introducing-...
0
votes
0
answers
220
views
How to set default property value of class type in C# System.Reflection.Emit
I am using System.Reflection in C# to create a dynamic object class at runtime.
This is the example object class that I want to create at runtime.
public class RequestForTest
{
public string ...
2
votes
1
answer
235
views
How to copy a method from an existing assembly to a dynamic assembly in .NET Core?
I want to somehow add a method from an on disk assembly to an assembly I am generating, I am creating the assembly via the System.Reflection.Emit and saving it to a file using the Lokad.ILPack nuget ...
0
votes
0
answers
81
views
Emit local variable which is not a standard type and assign a value to it
We can assign a variable using EMIT which has a standard type such as string, etc.
But how can I assign a non-standard type such as Type?
DynamicMethod dynamicMethod
= new ...
1
vote
1
answer
56
views
Why cannot I create a dynamic delegate which has more than one argument using .NET EMIT
There are some definations:
public class Message
{
public SayType Say(string name)
{
Console.Write("Hello," + name );
return SayType.Name;
}
public ...
1
vote
1
answer
60
views
Method overload emitted via dynamic IL is not being called
So I am attempting to create a class that will wrap up all the bits necessary to implement a single-instance application (SIA). It should be a drop-in replacement for Application.Run<T>().
...
1
vote
0
answers
146
views
How can I use System.Reflection.Emit to generate a delegate with special input and get the return value?
For example, we have such a class:
public class Message
{
public SayType Say(string name)
{
Console.Write("Hello," + name);
return SayType.Name;
}
}
public enum ...
1
vote
2
answers
81
views
How to change the .maxstack size of constructor and methods in dynamic assembly
I want to dynamically create an assembly using the functionality provided System.Reflection.Emit. The assembly should be identical to the assembly that results from compiling the following C# code:
...
0
votes
1
answer
183
views
"The invoked member is not supported before the type is created" in Reflection.Emit
I'm trying to learn Reflection.Emit by diving right in and what I'm stumped on at the moment is the following:
I have this PInvoke method
Type[] ptCryptEnumOIDInfo = { typeof(UInt32), typeof(UInt32), ...
0
votes
1
answer
63
views
How to do a circular dependency with System.Reflection.Emit
I have a generation framework that can generate dll using system.reflection.emit but have a case that i dont think can be solved with Emit, or atleast i have not figured out to do a minimal repro that ...
1
vote
0
answers
68
views
Unpack Array of args to the stack and call function with those args using Reflection.Emit
I am working on a c# project that uses Reflection.Emit.
I have the args on the stack for a function call to Dictionary.ByKeysValues(IList<string>,IList<object>), but must first transform ...
0
votes
1
answer
220
views
C# Getting a previously defined method builder from the type builder
Consider the following C# code:
var methodBuilder1 = typeBuilder.DefineMethod("GetMedalIds", MethodAttributes.Public | MethodAttributes.HideBySig, CallingConventions.HasThis, typeof(int[]), ...
0
votes
0
answers
125
views
.Net Emit - Add Try Catch block to existing method in runtime
I use the PubSub mechanism the send events in my application,
each event has a custom handler that use to handle it.
My problem is that in many places the handler suppled as the following:
...
1
vote
0
answers
119
views
Creating MetadataReference from in-memory assembly created with Reflection.Emit
I have created an assembly, module and classes in a dynamic assembly using Reflection.Emit in .NET6. I unable to reference that assembly in my CSharpCompilation; I can't use MetadataReference....
1
vote
0
answers
430
views
How add property at runtime to already existing class Reflection.Emit
I'm trying to add a field to an existing class, I found a solution through Reflection.Emit, but this output only works if we define a new class, and I need to add it to an existing one, and I can't ...
1
vote
0
answers
355
views
How to dynamically add an instance method to type from loaded assembly in runtime?
I have .NET 6.0 app which dynamically loads .NET Framework 4.8 assembly. Calling method from this assembly with reflection throws this exception:
System.MissingMethodException: Method not found:
'...
2
votes
2
answers
142
views
how create delegeate of a type which is not created
I'm using the system. reflection.Emit to implement google protocol message
I want to init a static field
class MessageA
{
static MessageParser<MessageA> Parser = new MessageParser<...
0
votes
1
answer
178
views
How to create dynamic function with unsure parameters in C# Emit
I want to create dynamic function in my project, but the number of function parameters is not sure,maybe two,maybe three or more,and then i want to run a static function already in the project,the ...
0
votes
1
answer
137
views
Get field value by using DynamicMethod
I'm trying to get the field value by using DynamicMethod instead of Reflection. If I change the field fldTest to static, the code works fine but I need also non-static field. When I run the code as ...
0
votes
0
answers
463
views
How to create a dynamic method in C# which uses an "in" parameter?
I try to create a dynamic method in .NET which has an "in" parameter, but it throws an exception in the EmitCall line:
public struct Context
{
public string MyString { get; set; }
}
...
2
votes
1
answer
141
views
Under what conditions can TypeBuilder.CreateType return null?
The TypeBuilder.CreateType() method is defined as nullable:
public Type? CreateType();
Under what conditions can it return null? The docs do not say.
I could dig into the source, but that would yield ...
0
votes
0
answers
47
views
Creating Dymamic Method throws en exception
I want to create some dynamic method whcih has 2 cycles: in first some N values are pushed onto evaluation stack, in second - these N values are popped from stack. But CreateMethod throws ...
0
votes
0
answers
332
views
How to translate this IL Code to Reflection.Emit in C#?
This is the IL code I'm trying to translate to C# without succes:
.assembly _
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = (...
1
vote
1
answer
241
views
How do I use TypeBuilder to create derived type with static field of base type?
I am having an issue using TypeBuilder to dynamically create a derived type that has a static field of the base type, that is initialized to a new instance of the created type.
Essentially, I want to ...
0
votes
1
answer
130
views
Getter method generated by System.Reflection.Emit fails to return primitive types, but works as expected for non-primitive objects
I'm trying to write a program that creates a dynamic "Wrapper" class around an existing class with properties, and redirects all the virtual properties getters and setters to a dedicated ...
0
votes
1
answer
1k
views
System.MissingMethodException: Method not found: '?' when trying to build a custom dynamic type with a delegate method
I am trying to dynamically build a type with a method that calls into an external delegate by using System.Reflection.Emit. However when I try to call this method, my program crashes with the ...
1
vote
1
answer
503
views
Generate interface implementation with Reflection.Emit for List of given properties
I am using code from this question to generate class from list of property
public class Field
{
public string FieldName;
public Type FieldType;
}
public static class MyTypeBuilder
{
...
0
votes
0
answers
78
views
How to dynamically build ctor
I'm trying to generate a class at runtime, but I'm struggling to create the ctor.
I have a base class that looks like this:
public abstract class MyServiceBase : SomeOtherBaseClass
{
private ...
1
vote
1
answer
159
views
Generating modopt using Reflection.Emit in Calli instruction
I'm trying to use Reflection.Emit to generate the code for Call method of following code:
public unsafe class Program
{
public struct ATest
{
public int Test;
}
public ...