Creating an anonymous object who must have the dot in Key name and put it inside another anonymous object

c# return anonymous object
c# anonymous object add property
list of anonymous type c#
c# anonymous type as parameter
convert anonymous type to list c#
return list of anonymous type c#
c# anonymous object to json
c# tuple

I'm making a JSON body for an elasticsearch query.

I have this dynamic:

var hlBodyText = new
{
  bodyText = new { }
};

But there's a case in which the name must be bodyText.exact = new { }, but obviously I'm not allowed to do it and return the error message:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

There's a way to make that value name with the dot char?

EDIT

Furthermore, I have to put this object inside another object, like this:

var fieldsInner = new
{
  hlBodyText.bodyText
};

What could be the best way to get this result but with the property name set with the dot?

EDIT #2

I created a class with all my parameters beacause I thought the JsonProperty attribute could help me.

internal class ElasticSearchHighlightsModel
{
  [JsonProperty("bodyText")]
  public object bodyText { get; set; }
  [JsonProperty("title")]
  public object title { get; set; }
  [JsonProperty("shortDescription")]
  public object shortDescription { get; set; }

  [JsonProperty("bodyText.exact")]
  public object bodyTextExact { get; set; }
  [JsonProperty("title.exact")]
  public object titleExact { get; set; }
  [JsonProperty("shortDescription.exact")]
  public object shortDescriptionExact { get; set; }
}

then in my method i have a condition for which I have to use some params or others.

// ...some code...
else
{
  var hlBodyText = new ElasticSearchHighlightsModel() { bodyTextExact = new { } };
  var hlTitle = new ElasticSearchHighlightsModel() { titleExact = new { } };
  var hlShortDescription = new ElasticSearchHighlightsModel() { shortDescriptionExact = new { } };

  var fieldsInner = new
  {
    hlBodyText.bodyTextExact,
    hlTitle.titleExact,
    hlShortDescription.shortDescriptionExact,
  };

  var fieldsContainer = new
  {
    pre_tags = preTags,
    post_tags = postTags,
    fields = fieldsInner,
  };
  return fieldsContainer;
}

But the fieldsInner object have the parameter names (bodyTextExact, titleExact etc...), not the JsonProperty attribute ones.

It seems this you are looking for,later u convert dictionary to json

Dictionary<string,object> obj=new Dictionary<string,object>();

obj["bodyText.exact"]=new object{};

Anonymous Types, Anonymous types in C# encapsulate a set of read-only properties in an object without having to explicitly define a type. The most common scenario is to initialize an anonymous type with properties from another type. You must provide a name for a property that is being initialized with an expression,� Given an array of values, I would like to create an anonymous object with properties based on these values. The property names would be simply "pN" where N is the index of the value in the array. For example, given. object[] values = { 123, "foo" }; I would like to create the anonymous object. new { p0 = 123, p1 = "foo" };

You seem to be creating an anonymous type (not "dynamic") and wanting to serialize it with a different name that is not valid in C#. To do that you'll need to use a named type and use the JsonProperty attribute:

internal class HlBodyText 
{
    [JsonProperty("bodyText.exact")]
    public DateTime bodyText { get; set; }
}

and create an instance of it:

var hlBodyText = new HlBodyText() 
{
  bodyText = new { }
};

C# Anonymous Types, In C#, an anonymous type is a type (class) without any name that can contain The following example demonstrates creating an anonymous type type. The properties can be accessed using dot (.) It cannot be returned from the method. If you need to pass it to another method, then use struct or class� To create a new anonymous type instance, car1, with Name and ID properties that are initialized with the values from the car object, you can write the following: VB. Dim car1 = New With {Key car.Name, Key car.ID} The previous declaration is equivalent to the longer line of code that defines anonymous type car2. VB.

Solved using Dictionary, then passed it inside an anonymous type obj:

IDictionary highlitsFieldsContainer = new Dictionary<string, object>();
// ... some code
highlitsFieldsContainer["bodyText.exact"] = new { };
highlitsFieldsContainer["title.exact"] = new { };
var fieldsContainer = new
{
  fields = highlitsFieldsContainer,
};

// OUTPUT: fieldsContainer = { fields = { bodyText.exact = {}, title.exact = {} }

And used a RouteValueDictionary class to read that values when elasticsearch send his response.

RouteValueDictionary _res = new RouteValueDictionary(dynamicResponse.highlights);
if (_res["shortDescription.exact"] != null)
{ 
  // ...
}

Anonymous Types In C#, Anonymous types allow us to create new types without defining them. In this As you can see from the below code sample, the type is store in a var and has two data items. Let's say, an object collection have properties FirstName, LastName, The compiler generates a name for each anonymous type. An anonymous object is essentially a value that has no name. Because they have no name, there’s no way to refer to them beyond the point where they are created. Consequently, they have “expression scope”, meaning they are created, evaluated, and destroyed all within a single expression. Here is the add() function rewritten using an

Return Anonymous Type in C#, Email; Bookmark; Print; Other Artcile. Expand If you want to return an Anonymous type from a method you must convert it to an object type. But that To understand each solution I created the following method that returns an anonymous type: And we need to remember the property name and type also. The basic form for declaring and instantiating an anonymous class is this: new ClassOrInterface () { class-body } As you can see, you specify the new keyword followed by the name of a class or interface that specifies the type of the object created from the anonymous class.

What is Anonymous Types in C#?, Anonymous types in C# are the types which do not have a name or you can say type which is inferred based on the data that you insert in an object initializer. to create a field, property, event, or return type of a method is of anonymous type. In C#, an anonymous type can have another anonymous type as a property. In C#, an anonymous type is a type (class) without any name that can contain public read-only properties only. It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax.

Anonymous classes - Manual, Anonymous classes are useful when simple, one-off objects need to be created. Nesting an anonymous class within another class does not give it access to any All objects created by the same anonymous class declaration are instances of This name has to be regarded an implementation detail, which should not be� TypeScript - Objects - An object is an instance which contains set of key value pairs. The values can be scalar values or functions or even array of other objects.

Comments
  • How do you convert this to Json?
  • I don't convert it, this is a little part of a big object I send to ES using Flurl
  • dynamic is not a type. Is this a local variable or a class property? If it's a property you can use the JsonProperty attribute
  • @DStanley is a local variable. Maybe create a class with properties could help
  • It's a good idea, but maybe that's not enough for my case. I added some informations on question
  • Not working yet. fieldsInner object have not the JsonParams attribute value but directly the class parameter name.