3

In C#, I need to create a function that returns an object which may possibly consist of:

{ itemID: "A1", itemDesc: "apple" } 

OR

{ groupID: "G1", groupDesc: "group1" } ..etc

// Please don't be concerned about why don't create another function for this,
// or why don't consider 'ID' instead.

I am curious. The reason, this is straightforward in javascript, as you can assign any key to an object without pre-defining it.

However, when it comes to C#, I am puzzled.

Is this possible?

Edit:

Sorry I should have stated by question clearer (so it may not be that 'duplicated')

My function actually returns a Class, as below:

public class MyClass 
{
    public int myKey {get;set;}
    public List<customClass> listRecord {get;set;}
}

The customClass is the dynamic "Object" that I refer to. Note that the records are queried from the LINQ.

Thanks for the suggestions, but I seem not able to do this:

var result = (from a in db.MyTable
              select new System.Dynamic.ExpandoObject()
              {
                 ....

Neither I can select new dynamic

7
  • 5
    objects in javascript are actually closer to Dictionarys in C#. Depending on usage, it may be more appropriate to return a Dictionary here. Or, don't try to write javascript in C#. Mar 24, 2017 at 6:54
  • The marked duplicate is the literal answer to your question. But I agree with @Damien that usage here may be better served using a dictionary. Of course, your question lacks much detail, so it's impossible to say what in your case would work best. Mar 24, 2017 at 7:31
  • @Peter Duniho, I have edited my question. I don't think this is considered 'Duplicated' as there are differences. But ya. as Damien mentioned, i would test using Dictionary
    – zeroflaw
    Mar 24, 2017 at 7:34
  • "I seem not able to do this" -- you need to be more specific. This question is likely a lost cause. The question you originally asked isn't actually the question you wanted an answer to. You need to post a new question in which you present your question clearly. Make sure you include in that post a good minimal reproducible example that shows what you've tried, and explain what that code does and what you want instead. Be sure to address why neither using a dictionary and ExpandoObject work for you, with specific details. Mar 24, 2017 at 7:44
  • @PeterDuniho I only mentioned ExpandoObject doesn't work, but not yet tested Dictionary.
    – zeroflaw
    Mar 24, 2017 at 10:27

3 Answers 3

8

You may need ExpandoObject class:

dynamic temp = new ExpandoObject();
temp.member1= "aha";
temp.member2= 1234;

https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx

Anonymous class can't be changed once defined.ExpandoObject is closer to javascript object.

3

For dynamic typing C# has a dynamic keyword, but it is strongly recommended not to use dynamic typing. Static typing allows you to check type on compilation and fail fast.

public dynamic Func()
{
    return new { itemID = "A1", itemDesc = "apple" };
}
2

you can do it with dynamic keyword:

public dynamic GetMyObject()
{
    return new { X= "X", Y= "Y" };
}

and You can use it with

dynamic myObject=GetMyObject();
myObject.X;
myObject.Y;

Not the answer you're looking for? Browse other questions tagged or ask your own question.