How to make JsonProperty() attribute have a dynamic propertyName? - c#

Essentially I have a Root class to serialize/deserialize JSON data that looks like this:
public class Root
{
[JsonIgnore]
public string JoystickName { get; set; }
public Root(string joystickName)
{
JoystickName = joystickName;
}
[JsonProperty("g")]
public string G { get; set; }
[JsonProperty(PropertyName = JoystickName)] // This line produces error
public DxsJoyConfig Joystick { get; set; }
}
*EDIT:
How do I change the property name at runtime?
I'm also running into an object reference error required at the indicated line. How can I make an instance of JoystickName within the attribute?

Related

Passing values from concretized instance of an base class to another base class instance

I am currently working on making viewmodels capable of parsing data extracted from database to the UI and vice versa, and to do so I do a lot of manual mapping between my two viewmodels.
Currently I try to pass some values that determines an attribute, but since each attributetype requires specifying a lot specific parameter, and 90% of the variables will be redundant in all cases since attributes only have one type..
Thus i have create a placeholder base class, which just contains an Id, that each atttribute have,
and each of the specific attribute type parameter will then use this placeholder as base class..
example:
public class BooleanViewSpecification : AttributeTypeSpecification
{
public string TrueOptionText { get; set; }
public string FalseOptionText { get; set; }
}
public class DateTimeTypeViewSpecification : AttributeTypeSpecification
{
public DateTime EarliestDataTime { get; set; }
public DateTime LatestDataTime { get; set; }
}
and my Attribute class is just an
public class AttributeView
{
public DataType Type { get; set; }
public AttributeTypeSpecification AttributeTypeViewSpecification { get; set; }
}
And the same Goes for my DB view model
public class BooleanSpecification : AttributeTypeSpecification
{
public string TrueOptionText { get; set; }
public string FalseOptionText { get; set; }
}
public class DateTimeTypeSpecification : AttributeTypeSpecification
{
public DateTime EarliestDataTime { get; set; }
public DateTime LatestDataTime { get; set; }
}
and my Attribute class is just an
public class Attribute
{
public DataType Type { get; set; }
public AttributeTypeSpecification AttributeTypeSpecification { get; set; }
}
Problem is then mapping from one class to another class
public static IEnumerable<AttributeView> MapToViewModel(this IEnumerable<Attribute> attributes)
{
return attributes.Select(z => new AttributeView()
{
Type = z.Type,
AttributeTypeViewSpecification = z.AttributeTypeSpecification
});
}
Which does not seem to work?
I use entity framework and migrate using Code-First what I receive is the Id of the location, and not the actual values?
I cant seem to understand why I cant be given the values - if it during the mapping does have the value?
So why cant they be mapped over?
I retrieve the value
Context.Include(Attribute).ThenInclude(AttributeTypeSpecification)
The only thing I receive is the actual Id rather than the specified entries?

Automapper's ProjectTo throws serilization error

I am trying to use Automapper's ProjectTo() extension method and I am running into an error:
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
The inner exception is:
Unable to create a null constant value of type 'DataContracts.StateContract'. Only entity types, enumeration types or primitive types are supported in this context.
Call I am making looks like this:
public IQueryable<OriginContract> Origins()
{
return contextProvider.Origins.ProjectTo<OriginContract>(automapperConfiguration);
}
And the classes I am using are like this:
Contracts:
public class OriginContract
{
public int OriginId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string City { get; set; }
public int StateId { get; set; }
public StateContract State { get; set; }
}
public class StateContract
{
public int StateId { get; set; }
public string Abbreviation { get; set; }
public string Name { get; set; }
}
The entities have the same properties and names (I copied and pasted from one to the other.) You can see them here if you like.
One important note, if I take out the State property from the Origin class, it works fine. So this error has something to do with trying to have a Origin class that contains a State class.
Any ideas how to get past this error?

Dynamic property to a C# object

I am thinking designing a field in db that stores the Serialized Object. When I call that property in entity, that returns String property which is obvious. I am looking for a way to attach a property dynamically and assign the deserialized object to the Class instance. Can any one suggest the best possible way?
DB Structure
Users Table
UserId ..... ..... ..... UserNotes (nvarchar)
Class Structure
[Serializable]
[XmlRoot("Notes")]
public class GenericNotes {
public DateTime Date {
get;
set;
}
public String CommentBy {
get;
set;
}
public string Type {
get;
set;
}
public string Comment {
get;
set;
}
}
public class Users {
public UserId int {
get;
set;
}
public string UserNotes {
get;
set;
}
// I dont have the following definition in the class because its coming from entity framework.
//But I want the following property attached to the class on runtime.
//I will take care of of deserializing using extension methods or some sort methods.
public string List < GenericNotes > NotesCollection {
get;
set;
}
}
Instead of property you can have extension method to do this
public static class UserExtension
{
public static List<GenericNotes> GetNotes(this Users users)
{
//return your deserialized GenericNotes from string
}
}
Then you can use this anywhere like
List<GenericNotes> notes = users.GetNotes();

WPF Toolkit PropertyGrid ExpandableObject attribute doesn't show nested class correctly

I'm using Extended WPF toolkit PropertyGrid to let users fill configurations in my application.
Everything works fine except when I try to include an ExpandableObject attribute to add a nested class.
Here is an example:
public class TestClassConfig
{
public string ExcelName { get; set; }
public string ResultFolder { get; set; }
[ExpandableObject]
public ExpandableTest OtherClass { get; set; }
}
public class ExpandableTest
{
public string Test1 { get; set; }
public string Test2 { get; set; }
}
I can't post an image of the result (first post), so i'll describe it: the "OtherClass" property appears but I can't see the nested class properties (Test1 and Test2) so cannot edit it.
In the PropertyGrid Documentation, it says for the property with ExpandableObject attribute that "This property is a complex property and has no default editor."
Does it means I have to create a custom Editor everytime I want to add a nested class to my property grid ?
Thanks for your answers!
So, It's will be rather usefull to know how you bind it in xaml.
I do it like this:
public class TestClassConfig
{
[Category("Cat1")]
public string ExcelName { get; set; }
[Category("Cat1")]
public string ResultFolder { get; set; }
[Category("CatWithExpandObj")]
[ExpandableObject]
public ExpandableTest OtherClass { get; set; }
}
public class ExpandableTest
{
public string Test1 { get; set; }
public string Test2 { get; set; }
}
And in xaml you mast bind Selected object:
<xctk:PropertyGrid SelectedObject="{Binding TestClassConfig}"/>
I know it is a late answer, however, make sure the value of the property is not null. In other words, write something like this:
public class TestClassConfig
{
[Category("CatWithExpandObj")]
[ExpandableObject]
public ExpandableTest OtherClass { get; set; } = new ExpandableTest();
}

Can Automapper map a complex source graph to a flat destination without prefixes in the destination properties and without custom mappings?

Is there a way to get Automapper to map a complex source graph like:
public class Source {
public string Name { get; set; }
public SourceSub Sub { get; set; }
}
public class SourceSub {
public string ValA { get; set; }
public string ValB { get; set; }
}
to a flat destination that looks like:
public class Dest {
public string Name { get; set; }
public string ValA { get; set; }
public string ValB { get; set; }
}
I know something like this will work for a destination:
public class Dest {
public string Name { get; set; }
public string SubValA { get; set; }
public string SubValB { get; set; }
}
However, I am looking for a way to map to the destination without requiring a prefix in the destination properties (for the child class in the source) as long as the names in the child class properties of the source match the destination property names. Is there a way to tell Automapper to project properties in a child class of the source to a flat destination class without mapping each individual member?
No, this isn't a supported scenario right now. We looked at it for a while, but found the naming collision rate too high for our apps, and having the name flattened preserved the full context for where that value came from.

Categories

Resources