-1

I want to create

public object Value { get; set; }

public Type Type { get; set; }

public string Name { get; set; } 

public string "user has to give propertyname" {get;set;}

at run time as per user requirement . Is it possible to do this in Csharp Using Expandos or Dynamics . I am asking that if i want to set property name in runtime line

public string "user has to give propertyname" {get;set;} .it must be from xml r sql but i want to set property name in runtime.

2 Answers2

0

I am not sure what you are asking for, but with Expandos you can do the following:

        dynamic d = new ExpandoObject();
        d.Name = "MyNameIsTest";
        d.Age = 26;
        d.Weight = 62.5d;
        d.dosomethingforme = "blablabla ....";
        d.GreetMe = new Action(delegate()
        {
            Console.WriteLine("Hello {0}", d.Name);
        });

and somewhere in your code you can have something like this:

    public void ResolveDynamic(dynamic obj)
    {
        Console.WriteLine(obj.Name);
        obj.Name = "Now I got a new name";

        Console.WriteLine(obj.dosomethingforme);

        obj.GreetMe();
    }

so if you call this function you'll get the following to see!

MyNameIsTest

blablabla ....

Hello Now I got a new name

I hope you can start something with this!

Cheers!

Gohlool
  • 363
  • 2
  • 5
  • 15
  • This Answer is not related with my question Read my question again. – aravind srinivas Feb 17 '12 at 09:41
  • so if you mean you want to change the name of a property of an existing object at run time, then I guess this is not possible! – Gohlool Feb 17 '12 at 09:53
  • This gives me error..how to solve this... class nameex { public object call() { dynamic expando = new ExpandoObject(); var p = expando as IDictionary; p["Demo"] = "New val 1"; } } class de { dynamic n = new nameex(); x.call(); n.Demo = 10; Console.WriteLine(n.Demo); – aravind srinivas Feb 20 '12 at 07:52
  • well, I guess this is exactly what you can not do! I've seen things like this done in JAVA. I mean adding new Properties into an existing Object. You are are calling dynamics n = new nameex() and trying to add a new property "Demo" to it and here is the error! I am not sure if you can solve that Problem like that! – Gohlool Feb 20 '12 at 10:27
  • Congrats! If you have any time just let us know how you solved this problem! Thanks and cheers – Gohlool Feb 20 '12 at 13:39
  • you have to make a override method to solve this problem if u r using dynamic object class its predefined class in c# . you can solve this problem http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trysetmember.aspx – aravind srinivas Feb 21 '12 at 04:52
0

If you want to add a property such that Reflection will give it to you, that's not possible. Once a class has been compiled there is no way to add members to it. If you just want to be able to set and get the property, though, that is the purpose of Expando and it will do that automatically.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Can u Explain me that how to create such a program – aravind srinivas Feb 20 '12 at 04:21
  • I'm afraid that I can't understand what you're trying to get your program to do, so I can't explain how to create a program to do it. – Gabe Feb 20 '12 at 06:30
  • class { dynamic n = new nameex(); nameex x = new nameex(); x.call(); n.Demo = 10; Console.WriteLine(n.Demo); } Class nameex { public object call() { dynamic expando = new ExpandoObject(); var p = expando as IDictionary; p["Demo"] = "New val 1"; } } This gives me error..how to solve this – aravind srinivas Feb 20 '12 at 07:41