How to add attributes to properties using XML in C#? - c#

I want to add attributes to properties of a class. I want to store attributes into an XML and read from there. I don't want to add them manually like this,
[CategoryIndex("1", "StackPanel")]
[FrameAttribute("abc")]
[PositionAttribute("0","1","0")]
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
I want to store all the attributes in an XML file and read from there.. How to proceed ?

This sounds like a job for the amazing ExpandoObject.
Excerpt:
You create an ExpandoObject instance as you do with any other .NET object, except that the variable to store the instance is of type dynamic:
dynamic expando = new ExpandoObject();
At this point, to add a property to the expando you simply assign it a new value, as below:
expando.FirstName = "Dino";
The link also includes saving and retrieving the properties to XML files.

Related

Deserialize JSON with empty attribute name

How can I DeserializeObject the following JSON string to a C# object
{"":["Waybill already exist"]}
In some instances the "" can contain a value as well
Like
{"RecevierAddress1" : ["Receiver address 1 can not be blank]}
Whereas what You ask is not possible in principle, because an object property must have a name, what you can do is convert it to a .net JsonDocument which can have properties of zero length string naming.
I presume RL data cause for you to have to handle this, which of cause indicates poor data quality besides that, but You should be able to process it using this technique, here from a unit test
[Fact]
public void SerializeSillyObjectJsonTest()
{
string serialized = "{\"\":[\"Waybill already exist\"]}";
var jdoc = System.Text.Json.JsonDocument.Parse(serialized);
Assert.NotNull(jdoc);
var jsonElement = jdoc.RootElement.GetProperty("");
Assert.Equal(1, jsonElement.GetArrayLength());
}
So You see you can also check on if your property with said name exist and choose what to look for
jdoc.RootElement.TryGetProperty("RecevierAddress1", out var receiverAddressElement)
You can use JsonProperty("") to set the property name to an empty string
class root
{
[JsonProperty("")]
public string[] x;
}
JsonConvert.DeserializeObject<root>(#"{"""":[""Waybill already exist""]}")
For dynamic names, you can either have two properties, or deserialize to a dictionary
JsonConvert.DeserializeObject<Dictionary<string, string[]>>(#"{"""":[""Waybill already exist""]}")

Anonymous object with key name with hyphen C# [duplicate]

Is it possible to create an object with a property name that contains a dash character?
I am creating an anonymous object so that I can serialize it to Json using Json.Net and one of the properties I need contains a '-' dash character.
An example of what I want is:
var document = {
condtions = new {
acl = "public-read",
bucket = "s3-bucketname",
starts-with = "test/path"
}
};
I know I could replace the dash with underscores when creating the object and then replace them in the serialized string afterwards, but wanted to know if there is a way in the language to do this without this workaround.
You can't do this with anonymous objects; field names must be valid identifiers. You could instead use a Dictionary, which Json.Net should serialise just as easily as an anonymous object:
var document = new {
conditions = new Dictionary<string, string>() {
{ "acl", "public-read" },
{ "bucket", "s3-bucketname" },
{ "starts-with", "test/path" }
}
};
Not in c#, no. However most serializers allow you to customise this - often via attributes. IIRC with JSON.NET you want [JsonProperty("starts-with")] to specify the name. However you can't use attributes on anonymous types, so you may need to define a class with the properties (and attributes) the you desire.
Unfortunately, that's not possible, because the language would not be able to differentiate the two following expressions:
condition.starts-with; // Read "starts-with" property.
condition.starts - with; // Read "starts" property and subtract "with" variable.

Using a Dictionary<string, Type> to use the value as type of a list

I am trying to build a solution fitting with the problem of not knowing what kind of Setting type I am dealing with.
I got a Dictionary<string, Type> (which I initially wanted to make <string, class> but that didn't work)
that I want to fill with the setting code and the type of class attached to it i.e.
{ "person_customField", typeof(CustomFieldModel) }
Why I want to do this is because I have a field in my database filled with json data that should be deserialized to a List<> but I don't know what kind of setting it is until I get the object from the database. I can use the Code field to detemine what type it is (person_CustomField should use the CustomFieldModel class, but emailSetting should use EmailSettingModel to match parameters to.
Is there a way to successfully make this statement work with?
JsonConvert.DeserializeObject<List<SettingTypes[record.SettingCode]>>(record.SettingValues).ToList<ISetting>()
Or should I go a different route
Code Sample:
public static readonly Dictionary<string, Type> SettingTypes = new Dictionary<string, Type>()
{
{ "person_CustomFields", typeof(CustomFieldModel)},
};
public static TenantSettingEdit ConvertToTenantSettingEdit(this T_TenantSetting rec)
{
var test = SettingTypes[rec.TENS_Code];
TenantSettingEdit item = new TenantSettingEdit()
{
IDToken = rec.TENS_TenantSettingID.toVirtualGuid().ToString(),
Code = rec.TENS_Code,
Settings = JsonConvert.DeserializeObject<List<SettingTypes[rec.TENS_Code]>>(rec.TENS_Setting).ToList<ITenantSetting>(),
IsActive = rec.TENS_ActiveRec,
};
return item;
}
(I have done this before with PHP but I am not sure if this is even remotely possible with C#)
Why I want to do this is because I have a field in my database filled
with json data that should be deserialized to a List<> but I don't
know what kind of setting it is until I get the object from the
database.
If you're using Json.Net for JSON serialization/deserialization you can use the TypeNameHandling property to embed Type information in the resulting JSON. That JSON can the be deserialized by Json.Net without additional information. If it is necessary to map custom values to the types instead of the automatically generated ones you can use a SerializationBinder (check out this answer).
If none of those help you, you can still fall back to reflection in the way M Kloster describes.
You cannot use a variable as the type parameter in the code, no. What you need to do is to generate the type-specific method by reflection:
var genericMethod = ((Func<string, int>)Json.DeserializeObject<int>).Method.GetGenericMethodDefinition();
var boundMethod = genericMethod.MakeGenericMethod(SettingTypes[record.SettingCode]);
var result = boundMethod.Invoke(null, rec.TENS_Setting)...

C# - Get value from dictionary with key - return property of object?

I'm making a program which when a chemical symbol of an element is entered, it'll return information of that element onto the form. This is simple to do but it want to try and keep my code as efficient as possible.
My dictionary:
Dictionary<string, object> alkaliM = new Dictionary<string, object>();
In my code also:
alkaliM.Add("Li", new AlkaliMetals.Lithium());
elementSymbol is my input string from the textbox
I'm trying to set elementName.Text to the property "name" of my AlkaliMetals.Lithium object. However, i can't simply put:
elementName.Text = alkaliM[elementSymbol.Text]; as i can't set an object as a string. ("Cannot implicty convert type "object" to "string"")
Is there any way to set elementName.Text to AlkaliMetals.Lithium.name using only the key?
I have a separate file that has a main class AlkaliMetals and subclasses for each of the alkali metals like Lithium.
If my description doesn't make sense let me know and i'll try to explain what i mean.
You can make all the sub classes in AlkaliMetals.cs implement an abstract class CommonAbstractClass
having the property name
Define you dictionary as Dictionary<string, CommonAbstractClass>
Then use your dictionary as follows elementName.Text = alkaliM[elementSymbol.Text].name;
You can find the name property (if you are absolutely sure it exists) using dynamic cast:
elementName.Text = (alkaliM[elementSymbol.Text] as dynamic).name
However, a more efficient approach would be Object Oriented Design:
Define a class named ChemicalElement, add name property to it and then derive Lithium from ChemicalElement. This way you can write:
elementName.Text = (alkaliM[elementSymbol.Text] as ChemicalElement).name
Best solution is to define your dictionary like this:
Dictionary<string, ChemicalElement> alkaliM = new Dictionary<string, ChemicalElement>();
And then access the name like:
elementName.Text = alkaliM[elementSymbol.Text].name

c# get attributes from object

I'm a bit new to C# and I got this script that gets a record from a mssql database. There it converts the key to an object.
Its:
Object obj = result[i];
When I enable a breakpoint on the line after the declaration I see the data that is inside the object. Now I need to access the attribute with the data but because Im a bit new to C# I dont know how to do that.
Lets say the attribute is called: name
I made a new class of my own also with the attribute name.
When I try to get the name of the key to my object with:
myObject.Name = (string) obj.Name;
The IDE already gives an error that the attribute in the obj isnt available.
How can I access the name attribute of the object to get it to my own object?
Thanks!
So result[i] is an instance of your class (which I'll call Foo for convenience)? Then you can say
Foo obj = result[i];
myObject.Name = obj.Name;
or
Object obj = result[i];
myObject.Name = ((Foo)obj).Name;
You need to cast your object to proper type. E.g. if your object is:
class MyObject
{
public string Name { get; set; }
}
than you need to cast it like:
MyObject obj = (MyObject)result[i];
you're trying to access an attribute of the Object class, and it has no attributes. You either have to cast your obj to the class that you created, the one that has the Name attribute, or (more simply), when reading the database read it directly into an instance of your class, something like:
MyClass obj = result[i]
What data is present in result[i]? Is it just a string from a field in the record in the database? (That is, is result a DataRow?) As it stands, you're placing it inside of just an Object which doesn't know much about it. Basically, you're "boxing" it and removing knowledge of the object's data from the compiler.
If result[i] is just a string, try something like:
myObject.Name = System.Convert.ToString(result[i]);
Now, this is fairly beginner in that there are other considerations to be made here. If result[i] is ever null then this will throw an exception, etc. But while you're learning this should get the data you're looking for.

Categories

Resources