How to dynamically Generate a class in C# [duplicate] - c#

This question already has answers here:
Generating DLL assembly dynamically at run time
(4 answers)
Closed 4 years ago.
I have json data whose name is Elements and it has one field Property.Now I want to add those Property inside a class and that class needed to be generated dynamically.
Here is the structure of my json data.
"Elements": [
{
"$type": "PCL.DatePicker, PCL",
"Type": "DatePicker",
"SelectedDate": "2017-02-23T00:00:00",
"LabelText": "Date:",
"Property": "Date1",
"Visibile": true
},
{
"$type": "PCL.FormEntryField, PCL",
"Type": "Entry",
"Text": "",
"LabelText": "Full Name:",
"Property": "FullName",
"Visibile": true
},
{
"$type": "PCL.Picker, PCL",
"Type": "Picker",
"DefaultIndex": 0,
"Values": [ "English", "Hindi", "French", "Chinese", "Arabi" ],
"LabelText": "Language",
"Property": "Language",
"Visibile": true
},
{
"$type": "PCL.FormSwitch, PCL",
"Type": "Switch",
"DefaultValue": true,
"Value": false,
"Text": null,
"Property": "Happy",
"LabelText": "Are You Happy?",
"Visibile": true
},
{
"$type": "PCL.TimePicker, PCL",
"Type": "TimePicker",
"SelectedTime": "12:30:00",
"LabelText": "Time:",
"Property": "TIme",
"Visibile": true
}
]
Is it possible to do this?

You could certainly use a T4 template to read through your JSON file and write out the c# class structure and properties.
That is assuming you will be generating at design time, similar to how entities are generated using edmx files.
Please see the Microsoft resource below for the basics of T4 templates.
msdn.microsoft.com/en-us/library/bb126445.aspx

Related

Multi-tab in message extension like shown in the microsoft documentation

How can I add multiple tab in message extension in Bot Framework using C# like as shown in this image here ?
enter image description here
You can add it by adding multiple search commands in the manifest file. Sharing part of JSON below.
"composeExtensions": [
{
"botId": "448ec85c-4395-4f80-b5a1-cd3bdefd1f5b",
"canUpdateConfiguration": true,
"commands": [
{
"id": "searchQuery",
"context": [
"compose",
"commandBox"
],
"description": "Test command to run query",
"title": "Search",
"type": "query",
"initialRun": true,
"parameters": [
{
"name": "searchQuery",
"title": "Search Query",
"description": "Your search query",
"inputType": "text"
}
]
},
{
"id": "searchQuery2",
"context": [
"compose",
"commandBox"
],
"description": "Test command to run query2",
"title": "Search2",
"type": "query",
"initialRun": true,
"parameters": [
{
"name": "searchQuery2",
"title": "Search Query2",
"description": "Your search query2",
"inputType": "text"
}
]
},
{
"id": "searchQuery3",
"context": [
"compose",
"commandBox"
],
"description": "Test command to run query",
"title": "Search3",
"type": "query",
"initialRun": true,
"parameters": [
{
"name": "searchQuery3",
"title": "Search Query3",
"description": "Your search query3",
"inputType": "text"
}
]
}
]
Here is the sample - https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/50.teams-messaging-extensions-search

C#, JSON, Deserialize into DataTable

I have my JSON as
{{
"attributes": {
"type": "User",
"url": "/services/data/v44.0/sobjects/User/0052F000002bO32QAE"
},
"Name": "demo Site Guest User",
"UserType": "Guest",
"Profile": {
"attributes": {
"type": "Profile",
"url": "/services/data/v44.0/sobjects/Profile/00e2F000000DuoFQAS"
},
"Id": "00e2F000000DuoFQAS",
"Name": "demo Profile"
}
}}
{{
"attributes": {
"type": "User",
"url": "/services/data/v44.0/sobjects/User/00541000006I1JyAAK"
},
"Name": "Scheduler Site Guest User",
"UserType": "Guest",
"Profile": {
"attributes": {
"type": "Profile",
"url": "/services/data/v44.0/sobjects/Profile/00e41000000GhKdAAK"
},
"Id": "00e41000000GhKdAAK",
"Name": "Scheduler Profile"
}
}}
In the above JSON, I want to fetch parent elements 'Name' and 'UserType' and child elements'Id' and 'Name' and convert into datatable. These parent and child elements are anonymous so I can't create classes for those.
I got up to here
dynamic responseRecs = (responseDetails.records as IEnumerable<object>).Cast<object>().ToList();
which is giving me the response but not sure how to loop through the dynamic and get parent and child line items (ignoring the type and url) so that I can build a datatable.
Any help will be greatly appreciated. Thanks

NJsonSchema: JSON schemas with nested arrays generate incorrect C# types

I would like to use NJsonSchema to generate C# classes. The problem is that for nested arrays, the type ends up looking like this:
System.Collections.ObjectModel.ObservableCollection<System.Tuple<System.Linq.Enumerable+WhereSelectEnumerableIterator2[NJsonSchema.JsonSchema4,System.String]>>
My test code for generating the classes looks like this:
var schemaAsync = NJsonSchema.JsonSchema4.FromFileAsync(
#"<myPath>/MyFile.json");
var schema = schemaAsync.Result;
var generator = new NJsonSchema.CodeGeneration.CSharp.CSharpGenerator(schema);
var file = generator.GenerateFile();
System.IO.File.WriteAllText(
#"<myPath>/SomeClass.cs",
file);
I have two JSON schema files, there's the definition file (Def1.json):
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"InnerList": {
"description": "Only ever has 2 items",
"type": "array",
"items": [
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
},
{
"type": "integer",
"minimum": 0,
"maximum": 100,
"default": 0
}
],
"additionalItems": false
},
"OuterList": {
"type": "array",
"items": {
"$ref": "#/definitions/InnerList"
}
},
}
}
And a separate schema that makes use of the definition file:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "MySchema",
"type": "object",
"required": [ "OuterList" ],
"properties": {
"OuterList": { "$ref": "Def1.json#/definitions/OuterList" }
},
"additionalProperties": false
}
Am I using the library incorrectly?
This problem has been fixed in the latest version.

Create DropDown list for jira custom field in REST API

I'm trying to retrieve the available options for fields through the Jira REST API but I'm getting stuck on the custom fields.
A normal field returns something like this:
{[priority, {
"required": false,
"schema": {
"type": "priority",
"system": "priority"
},
"name": "Priority",
"key": "priority",
"hasDefaultValue": true,
"operations": [
"set"
],
"allowedValues": [
{
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/1",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/highest.svg",
"name": "Highest",
"id": "1"
},
{
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/2",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/high.svg",
"name": "High",
"id": "2"
},
{
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/3",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/medium.svg",
"name": "Medium",
"id": "3"
},
{
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/4",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/low.svg",
"name": "Low",
"id": "4"
},
{
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/5",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/lowest.svg",
"name": "Lowest",
"id": "5"
}
],
"defaultValue": {
"self": "https://inn-inw.atlassian.net/rest/api/2/priority/3",
"iconUrl": "https://inn-inw.atlassian.net/images/icons/priorities/medium.svg",
"name": "Medium",
"id": "3"
}
}]}
The custom fields on the other hand return a much "smaller" set!
"Sprint" Custom Field:
{[customfield_10113, {
"required": false,
"schema": {
"type": "array",
"items": "string",
"custom": "com.pyxis.greenhopper.jira:gh-sprint",
"customId": 10113
},
"name": "Sprint",
"key": "customfield_10113",
"hasDefaultValue": false,
"operations": [
"set"
]
}]}
"Epic Link"
{[customfield_10006, {
"required": false,
"schema": {
"type": "any",
"custom": "com.pyxis.greenhopper.jira:gh-epic-link",
"customId": 10006
},
"name": "Epic Link",
"key": "customfield_10006",
"hasDefaultValue": false,
"operations": [
"set"
]
}]}
As you can see there are no options available, but i know that there are.
How can I get to these values through rest?
I have tried checking the request that the cloud server makes but it's pretty different for each of the custom fields and i cannot find a relation between them. For example:
To get the sprint possible values: https://inn-inw.atlassian.net/rest/greenhopper/1.0/sprint/picker?query=
and to get the Epic values: https://inn-inw.atlassian.net/rest/greenhopper/1.0/epics
I'm trying to make things as clean as possible and I would prefer not to hard code the request, specially because i would probably need to update the code every time a new custom field is created.
PS: I have tried some of the solutions presented in other questions but couldn't find one that fits what i need. eg: https://inn-inw.atlassian.net//rest/api/2/issue/createmeta?projectKeys=PROJKEY&issuetypeNames=Bug&expand=projects.issuetypes.fields returns only the short description that is displayed above.

Timeout when indexing document with custom analyzer

I am creating the mappings for an index I will be using in a project.
Given the domain of the features, I'd like most of the fields to be searchable through case-insensitive term queries.
I had worked through a custom analyzer (like the one suggested here: Elasticsearch Map case insensitive to not_analyzed documents) but when I try to index a document, the process hangs for 60 seconds until a timeout happens and the whole process fails.
I see the same behavior when I test on Sense.
Here is the index definition:
put /emails
{
"mappings": {
"email": {
"properties": {
"createdOn": {
"type": "date",
"store": true,
"format": "strict_date_optional_time||epoch_millis"
},
"data": {
"type": "object",
"dynamic": "true"
},
"from": {
"type": "string",
"store": true
},
"id": {
"type": "string",
"store": true
},
"sentOn": {
"type": "date",
"store": true,
"format": "strict_date_optional_time||epoch_millis"
},
"sesId": {
"type": "string",
"store": true
},
"subject": {
"type": "string",
"store": true,
"analyzer": "standard"
},
"templates": {
"properties": {
"html": {
"type": "string",
"store": true
},
"plainText": {
"type": "string",
"store": true
}
}
},
"to": {
"type": "string",
"store": true
},
"type": {
"type": "string",
"store": true
}
}
},
"event": {
"_parent": {
"type": "email"
},
"properties": {
"id": {
"type": "string",
"store": true
},
"origin": {
"type": "string",
"store": true
},
"time": {
"type": "date",
"store": true,
"format": "strict_date_optional_time||epoch_millis"
},
"type": {
"type": "string",
"store": true
},
"userAgent": {
"type": "string",
"store": true
}
}
}
},
"settings": {
"number_of_shards": "5",
"number_of_replicas": "0",
"analysis": {
"analyzer": {
"default": {
"tokenizer": "keyword",
"filter": [
"lowercase"
],
"type": "custom"
}
}
}
}
}
As you can see, I define an analyzer as "default" (if I try to use another name and define it as a default analyzer for each of the two types, I get a "Root mapping definition has unsupported parameters: [analyzer : my_analyzer]" error).
And this is me trying to add a document to the index
post /emails/email/1
{
"from": "email-address-1",
"to": "email-address-2",
"subject": "Hello world",
"data":{
"status": "SENT"
}
}
I really can't understand why this timeout is happening.
I also tried using NEST via a C# console application. Same behavior.
Thanks.
PS: for testing I am using both Elasticsearch 2.3 hosted by AWS and Elasticsearch 2.3 hosted in a local docker container.
The problem is that you have 1 node and an index with 1 primary shard and 5 replica shards.
Since replicas of a primary will not be assigned on the same node as the primary, the 5 replicas will all be unassigned. This is an issue when indexing a document; by default, the write consistency for an index operation is quorum, and a quorum of 6 (1 primary + 5 replicas) is 4 (n/2 + 1). This means the document needs to have been written to the primary and 3 replicas of the same shard in order to succeed. With unassigned shards, it won't be possible to satisfy this. You'll see a UnavailableShardsException in the logs with an error message for this.
Changing your index to 5 shards and 1 replica will solve the problem.

Categories

Resources