Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi
I try to create dynamic method as

C#
public void SetId(Guid id)
{
  Id = id;
}


I try but Id is private set

C#
var DynamicMethod = new DynamicMethod("Division", typeof(void), new Type[] { typeof(Guid) }, entityType.Module);
var il = DynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, entityType.GetProperty("Id").GetSetMethod());
il.Emit(OpCodes.Ret);


how can i do this

What I have tried:

DynamicMethod = new DynamicMethod("Division", typeof(void), new Type[] { typeof(Guid) }, entityType.Module);
var il = DynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, entityType.GetProperty("Id").GetSetMethod());
il.Emit(OpCodes.Ret);
Posted
Comments
Sergey Alexandrovich Kryukov 9-Feb-16 16:01pm    
Before trying to answer any questions, I would like to ask you: what's the goal of using a dynamic method? I already spent too much time on explaining System.Reflection.Emit to some who did not really need it.

The question for you is: yes, you already created a dynamic method. So, what's the problem? Don't know how to call it, or anything else? It's clearly described in MSDN on dynamic methods. Any other problems? Using IL with Emit is difficult enough, because the code is hard to debug...

—SA
Oguz Türkan 10-Feb-16 0:24am    
Hi, thanks for reply. I have class and Id property is readonly. I try to set Id. When I create class with Activator.CreateInstance<t> there is no consructor parameter. Also I tried to Activator.CreateInstance(type, params) but My Class dosesnot have consructor. also is inherited from abstract class. I dont want to add constructor for all classes.
Sergey Alexandrovich Kryukov 10-Feb-16 1:59am    
First of all, you had to comment on my comment; without it, I did not get a notification. You commented on your own post.

Dynamic methods do not require any constructors. I am not sure if you understand the idea. This is yet another reason for my to insist on telling your ultimate goal; I'm not sure that you are not misusing reflection or System.Reflection.Emit.

Now, why would you use Activator at all? This is a totally different topic, not related to *.*.Emit. Who told you that an activator can create a class (System.Reflection.Emit could, yes)? It creates only some object (instance of type) using, for example, the type name...

—SA
BillWoodruff 10-Feb-16 2:16am    
You need to clarify what your goal is here: creating a Type at run-time, or creating an instance of a Type at run-time.

If you have this:
C#
using System;

namespace YourNameSpace
{
    public abstract class PersonBase
    {
        public PersonBase(Guid id)
        {
            Id = id;
        }

        // hide from exposure outside 'ctor or 'ResetID method
        internal Guid Id { private set; get; }

        internal void ResetID(Guid id)
        {
            Id = id;
        }
    }
 
    public class Person : PersonBase
    {
        public Person(Guid id) : base(id)
        {
        }

        public Person() : base(Guid.Empty)
        {
        }
    }
}
You can do this to use 'Activator.CreateInstance, and pass a value for the 'Guid parameter:
C#
Person somePerson = Activator.CreateInstance(typeof (Person), new Object[]{Guid.NewGuid()}) as Person;
Hope that's helpful. If you are doing a lot fancy composition of .NET objects at run-time, I suggest you research the use of T4: [^]
 
Share this answer
 
Comments
Oguz Türkan 12-Feb-16 19:53pm    
Thank you very helpfull.
Thanks for replay. Sample code is below

C#
public abstract class Base
{
   public readonly Id;
}

public class Person : Base
{
}


I create Person Instance like Activator.CreateInstance<t>()

i want to set Id. Any suggestion ?

thanks
 
Share this answer
 
Comments
BillWoodruff 10-Feb-16 6:40am    
This is not a solution, but information that clarifies your original question. If you don't delete this, you may get down-voted, or reported.

Note you don't specify the type for 'Id, so this won't compile.

I suggest you revise your original question to include this information.

By the way, it is still not clear to me, even with this example, of what exactly you want to do. If you declare a property 'public and 'readonly, then the only way to set that property is in the Constructor, or in a 'public method in the Class.
Philippe Mori 12-Feb-16 22:22pm    
It does not make any sense to declare a field read only if you want to set it from outside its declaration or class constructor.

By the way, this is not an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900