1

The question is about how to add some property to an object message without using inheritance and without changing base type. Here is what in my mind: Just suppose I have an object message to carry my message from service layer to front-end like this:

To clearance for you dear developers, I am not allowed inherit my result from any interface or base class because as you see my result type is object I want to keep it object and client which using these supposed the result is an object. We don't want change any thing in client side

public object DoSomething(param parameters)
{
    .
    list<MyObject> result= ...
    return result;
}

Ok, Now in front-end I have:

public object GetResult(param parameters)
{
    ...
    return _service.DoSomething(parameteres);
}

usage:

{
        .
        .
        var result=GetResult(...);
        if (**result.Status**==Success)
        {
            .
            foreach(var item on result)
            {
               ...
            }
        }
}

as you see for example I want to have a property like 'status' on my result or any other properties. I think using the something like proxy or some Aspect Programming techniques may help me What I need to add a property at runtime to an existing object - WITHOUT CHANGE OBJECT BASE CLASS OR IMPLEMENT ANY OTHER INTERFACE

Mohammad Nikravesh
  • 947
  • 1
  • 8
  • 27
  • use [dynamic](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic) – tchelidze Aug 08 '17 at 13:40
  • @tchelidze How would that help? – DavidG Aug 08 '17 at 13:40
  • Why can't you use inheritance or change the base type? The only "simple" way I can think that might work is to wrap the object in a custom type. – DavidG Aug 08 '17 at 13:42
  • Is `Status` something that can be inferred from other properties of the returned object? If so you could create an extension class that looks at the returned object and determines the status from its properties. – Scott Hannen Aug 08 '17 at 13:43
  • Possible duplicate of [Dynamically Add C# Properties at Runtime](https://stackoverflow.com/questions/15819720/dynamically-add-c-sharp-properties-at-runtime) – Clint Aug 08 '17 at 13:53
  • If the method can't return a value due to some exception (not the same as returning *no* data) then you can just throw an exception. If it's truly unexpected because something fails then an exception is a valid response. If there's no exception then that's success. If there's no data - not the same as an exception - return an empty object that the consumer can correctly interpret as empty. – Scott Hannen Aug 08 '17 at 15:13
  • @ScottHannen not I don't want inherit my class from any interface or base class – Mohammad Nikravesh Aug 09 '17 at 09:58
  • @ScottHannen Throwing exception is not my solution Mr.Hannen. I really need to have a dynamic property at runtime to add a lot of data, The status is just a sample – Mohammad Nikravesh Aug 09 '17 at 10:00
  • Hey come on guys, why my question got 2 negative point :| Please give me vote I would appreciate. StackOverFlow doesn't allow me to post new question because of this :| – Mohammad Nikravesh Nov 07 '17 at 02:02

2 Answers2

0

Did you consider using C# extension in which you can add a method to the object type? Yes, I know you wanted properties but it not allowed to extend properties in C# yet: Does C# have extension properties?

Zhuravlev A.
  • 386
  • 4
  • 12
-1

One way to do this, would be to use an ExpandoObject. You can create your object as expandable one, this way you will be able to add new properties on the fly

    using System.Collections.Generic;
using System.Dynamic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic result = GetResult();
            if (result.Status == "Success")
            {
                foreach (var item in result.list)
                {

                }
            }
        }

        public static ExpandoObject DoSomething()
        {

            dynamic result = new ExpandoObject();
            result.list = new List<MyObject>();
            return result;
        }

        public static ExpandoObject GetResult()
        {
            return DoSomething();
        }
    }

    internal class MyObject
    {
    }
}

here is the code to show you how you can do it

Vijay Parmar
  • 795
  • 4
  • 13
  • 1
    There's no such thing as "Expandable" object. There is an `ExpandoObject` though. Also, reflection won't let you add properties. – DavidG Aug 08 '17 at 13:59
  • 1
    @DavidG pls refere this https://stackoverflow.com/questions/14724822/how-can-i-add-properties-to-a-class-on-runtime-in-c – Vijay Parmar Aug 08 '17 at 14:01
  • 1
    No, that's just creating a new class, not adding to an existing one. – DavidG Aug 08 '17 at 14:03
  • @vijayparmar this method may be work on object and classes but I think you can't using ExpandoObject to add some property to a list<> – Mohammad Nikravesh Aug 09 '17 at 10:02