Add different attributes to derived classes # - c#

I have a base class named event and 2 subclasses named sendEvent and receiveEvent. You can see the code below:
namespace App
{
public class Event
{
public Type type { get; set; }
public Details details { get; set; }
}
public class Details
{
public string timestamp { get; set; }
public string reference { get; set; }
public string id { get; set; }
public string correlator { get; set; }
public string device1 { get; set; }
public string device2 { get; set; }
public string device3 { get; set; }
}
public class Details
{
public string timestamp { get; set; }
public string reference { get; set; }
public string primaryid { get; set; }
public string primary_correlator { get; set; }
public string secondaryid { get; set; }
public string secondary_correlator { get; set; }
public string device4 { get; set; }
public string device5 { get; set; }
}
class ReceiveEvent : Event
{
public ReceiveEvent()
{
this.type = Type.Recieve;
}
}
class SendEvent : Event
{
public SendEvent()
{
this.type = Type.Send;
}
}
public enum Type
{
Send,
Receive
}
}
I want that the sendEvent use first details while the receiveEvent use second details class. I couldn't figure out how can I make it possible. Do you have any thoughts?

One option would be to separate the common fields into a base class and create subclasses:
public class Details
{
public string timestamp { get; set; }
public string reference { get; set; }
}
public class SendDetails : Details
{
public string id { get; set; }
public string correlator { get; set; }
public string device1 { get; set; }
public string device2 { get; set; }
public string device3 { get; set; }
}
public class ReceiveDetails : Details
{
public string primaryid { get; set; }
public string primary_correlator { get; set; }
public string secondaryid { get; set; }
public string secondary_correlator { get; set; }
public string device4 { get; set; }
public string device5 { get; set; }
}
Then make your Event classes generic:
public class Event<TDetail> where TDetail : Details
{
public Type type { get; set; }
public TDetail details { get; set; }
}
public class ReceiveEvent : Event<ReceiveDetails>
{
public ReceiveEvent()
{
this.type = Type.Recieve;
}
}
public class SendEvent : Event<SendDetails>
{
public SendEvent()
{
this.type = Type.Send;
}
}
That way you can access details in a strongly-typed manner from either the base event class or the subclasses.

You can create a base class Details and derive subclasses like SendDetails and ReceiveDetails from it:
namespace App
{
public class Event
{
public Type type { get; set; }
public Details details { get; set; }
}
public class Details
{
public string timestamp { get; set; }
public string reference { get; set; }
}
public class SendDetails : Details
{
public string id { get; set; }
public string correlator { get; set; }
public string device1 { get; set; }
public string device2 { get; set; }
public string device3 { get; set; }
}
public class ReceiveDetails : Details
{
public string primaryid { get; set; }
public string primary_correlator { get; set; }
public string secondaryid { get; set; }
public string secondary_correlator { get; set; }
public string device4 { get; set; }
public string device5 { get; set; }
}
class ReceiveEvent : Event
{
public ReceiveEvent()
{
this.type = Type.Recieve;
this.Details = new ReceiveDetails();
}
}
class SendEvent : Event
{
public SendEvent()
{
this.type = Type.Send;
this.Details = new SendDetails();
}
}
public enum Type
{
Send,
Receive
}
}

If you want to use single class Event you can use Generics:
public class Event<TDetails>
{
public Type type { get; set; }
public TDetails details { get; set; }
}
class SendEvent : Event<SendDetails>
{
public SendEvent()
{
this.type = Type.Send;
}
}

Related

DynamoDB how to use ScanAsync on a nested structure?

Anyone have any idea / example how can I get the list of DynamoDbRepo with all nested structure if in DynamoDbMethodParameter.Name == specific value?
I suppose I should use ScanAsync but with what setting( ScanRequest ) ?
[DynamoDBTable("REPO_TABLE")]
public class DynamoDbRepo
{
[DynamoDBProperty("Id")]
[DynamoDBHashKey]
public int Id { get; set; }
[DynamoDBProperty("Solutions")]
public List<DynamoDbSolution> Solutions { get; set; }
}
public class DynamoDbSolution
{
[DynamoDBProperty("Path")]
public string Path { get; set; }
[DynamoDBProperty("Methods")]
public List<DynamoDbMethod> Methods { get; set; }
}
public class DynamoDbMethod
{
[DynamoDBProperty("Name")]
public string Name { get; set; }
[DynamoDBProperty("MethodParameters")]
public List<DynamoDbMethodParameter> MethodParameters { get; set; }
}
public class DynamoDbMethodParameter
{
[DynamoDBProperty("Type")]
public string Type { get; set; }
[DynamoDBProperty("Name")]
public string Name { get; set; }
}

How to remove $ for json result?

I am trying to read a cell value from a simple google sheet
https://docs.google.com/spreadsheets/d/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/edit?usp=sharing
then I published it and I get an API link that return JSON, check the following
https://spreadsheets.google.com/feeds/list/1opP1t_E9xfuLXBkhuyzo5j9k_xBNDx0XKb31JwLP1MM/1/public/values?alt=json
when I tried to generate C# classes from JSON using http://json2csharp.com/, I get
invalid_name and $ (which is not fine for csharp compiler)
public class Id
{
public string __invalid_name__$t { get; set; }
}
public class Updated
{
public DateTime __invalid_name__$t { get; set; }
}
public class Category
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class Name
{
public string __invalid_name__$t { get; set; }
}
public class Email
{
public string __invalid_name__$t { get; set; }
}
public class Author
{
public Name name { get; set; }
public Email email { get; set; }
}
public class OpenSearchTotalResults
{
public string __invalid_name__$t { get; set; }
}
public class OpenSearchStartIndex
{
public string __invalid_name__$t { get; set; }
}
public class Id2
{
public string __invalid_name__$t { get; set; }
}
public class Updated2
{
public DateTime __invalid_name__$t { get; set; }
}
public class Category2
{
public string scheme { get; set; }
public string term { get; set; }
}
public class Title2
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Content
{
public string type { get; set; }
public string __invalid_name__$t { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string type { get; set; }
public string href { get; set; }
}
public class GsxName
{
public string __invalid_name__$t { get; set; }
}
public class GsxPhonenumber
{
public string __invalid_name__$t { get; set; }
}
public class Entry
{
public Id2 id { get; set; }
public Updated2 updated { get; set; }
public List<Category2> category { get; set; }
public Title2 title { get; set; }
public Content content { get; set; }
public List<Link2> link { get; set; }
public GsxName __invalid_name__gsx$name { get; set; }
public GsxPhonenumber __invalid_name__gsx$phonenumber { get; set; }
}
public class Feed
{
public string xmlns { get; set; }
public string __invalid_name__xmlns$openSearch { get; set; }
public string __invalid_name__xmlns$gsx { get; set; }
public Id id { get; set; }
public Updated updated { get; set; }
public List<Category> category { get; set; }
public Title title { get; set; }
public List<Link> link { get; set; }
public List<Author> author { get; set; }
public OpenSearchTotalResults __invalid_name__openSearch$totalResults { get; set; }
public OpenSearchStartIndex __invalid_name__openSearch$startIndex { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject
{
public string version { get; set; }
public string encoding { get; set; }
public Feed feed { get; set; }
}
I want to serialize and deserialize this class, also i want to remove $,what i need to do?
Obviously json2csharp converts each json object into a class and converts the key names into variable names literally. So whenever it finds a key name starting with $ it can't create a c# variable with this character and it precedes the variable name with a _invalid_name_. There is nothing wrong here.
You should tell why do you want to remove this invalid_name phrase from variable name? do you want to serialize and deserialize this class? If so you could use NewtonSoft Json library and define those fields with $ sign like this:
[JsonProperty(PropertyName = "$t")]
public string t { get; set; }
this will allow you to serialize/deserialize the json doc
same for gsx$name:
[JsonProperty(PropertyName = "gsx$name")]
public string gsxname { get; set; }

Cannot Deserialize JSON Object type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

I'm having some issues handling a JSON array, Here is what I've tried. I have tried also using <List<jsonResponse.RootObect> and get the same result.
I'm using JSON.NET
C#:
jsonResponse.RootObject deserializedResponse = JsonConvert.DeserializeObject<jsonResponse.RootObject>(Globals.jsonResponse);
CLASS:
namespace QuantumView
{
[JsonObjectAttribute]
class jsonResponse
{
public class TransactionReference
{
public string CustomerContext { get; set; }
}
public class Response
{
public TransactionReference TransactionReference { get; set; }
public string ResponseStatusCode { get; set; }
public string ResponseStatusDescription { get; set; }
}
public class SubscriptionStatus
{
public string Code { get; set; }
public string Description { get; set; }
}
public class StatusType
{
public string Code { get; set; }
public string Description { get; set; }
}
public class Address
{
public string AddressLine1 { get; set; }
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
}
public class Shipper
{
public string Name { get; set; }
public string ShipperNumber { get; set; }
public Address Address { get; set; }
}
public class Address2
{
public string ConsigneeName { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string StateProvinceCode { get; set; }
public string PostalCode { get; set; }
public string CountryCode { get; set; }
}
public class ShipTo
{
public string AttentionName { get; set; }
public string PhoneNumber { get; set; }
public Address2 Address { get; set; }
}
public class ReferenceNumber
{
public string Number { get; set; }
public string Value { get; set; }
}
public class Service
{
public string Code { get; set; }
}
public class Activity
{
public string Date { get; set; }
public string Time { get; set; }
}
public class Dimensions
{
public string Length { get; set; }
public string Width { get; set; }
public string Height { get; set; }
}
public class UnitOfMeasurement
{
public string Code { get; set; }
}
public class DimensionalWeight
{
public UnitOfMeasurement UnitOfMeasurement { get; set; }
public string Weight { get; set; }
}
public class PackageWeight
{
public string Weight { get; set; }
}
public class ReferenceNumber2
{
public string Number { get; set; }
public string Value { get; set; }
}
public class PackageServiceOptions
{
public string COD { get; set; }
}
[JsonArray]
public class Package
{
public Activity Activity { get; set; }
public Dimensions Dimensions { get; set; }
public DimensionalWeight DimensionalWeight { get; set; }
public PackageWeight PackageWeight { get; set; }
public string TrackingNumber { get; set; }
public List<ReferenceNumber2> ReferenceNumber { get; set; }
public PackageServiceOptions PackageServiceOptions { get; set; }
}
public class BillToAccount
{
public string Option { get; set; }
public string Number { get; set; }
}
[JsonArray]
public class Manifest
{
public Shipper Shipper { get; set; }
public ShipTo ShipTo { get; set; }
public List<ReferenceNumber> ReferenceNumber { get; set; }
public Service Service { get; set; }
public string PickupDate { get; set; }
public string ScheduledDeliveryDate { get; set; }
public string ScheduledDeliveryTime { get; set; }
public string DocumentsOnly { get; set; }
public Package Package { get; set; }
public string ShipmentChargeType { get; set; }
public BillToAccount BillToAccount { get; set; }
}
public class SubscriptionFile
{
public string FileName { get; set; }
public StatusType StatusType { get; set; }
public List<Manifest> Manifest { get; set; }
public object Origin { get; set; }
}
This is where I'm getting the error..
[JsonArray]
public class SubscriptionEvents
{
public string Name { get; set; }
public string Number { get; set; }
public SubscriptionStatus SubscriptionStatus { get; set; }
public List<SubscriptionFile> SubscriptionFile { get; set; }
}
public class QuantumViewEvents
{
public string SubscriberID { get; set; }
public SubscriptionEvents SubscriptionEvents { get; set; }
}
public class QuantumViewResponse
{
public Response Response { get; set; }
public QuantumViewEvents QuantumViewEvents { get; set; }
public string Bookmark { get; set; }
}
public class RootObject
{
public QuantumViewResponse QuantumViewResponse { get; set; }
}
}
}
The problem was with my class not having the correct properties, it also turns out that the UPS api doesn't send a static type response and it can be different each time.. requiring a new class to deserialize into. I have not found a way to make the class flexible

How to check whether an object has certain method/property of particular type within it?

I have DInfo class present in two different namespaces i.e. ABC.Domain and ABC.Common
I am getting xml body as a record from database from which I am deserializing to respective type.
I have to find out all the records which are using properties with name/names of properties of type ABC.Domain.DInfo and just ignore of type ABC.Common.DInfo
As a I am getting record of type IEvent i.e may be FSubmitted or GSubmitted
namespace ABC.Domain
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
namespace ABC.Common
{
public class DInfo
{
public DateTime? Date { get; set; }
public URef User { get; set; }
public Decimal? L1 { get; set; }
public Decimal? L2 { get; set; }
}
}
public class Event : IEvent
{
public Guid Id { get; set; }
public Event() { }
public int Number { get; set; }
}
public interface IEvent : IRBase
{
Guid Id { get; set; }
int Number { get; set; }
}
public interface IRBase
{
string RUser { get; set; }
string Sub { get; set; }
}
public abstract class REventBase : Event
{
public Guid Id { get; set; }
}
public class FSubmitted : REventBase
{
public RSummary NewForm { get; set; }
}
public class GSubmitted : REventBase
{
public FRef NewForm { get; set; }
}
public class RSummary
{
public Guid ID { get; set; }
public FRef FRef { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Common.DInfo Saved { get; set; }
public ABC.Domain.DInfo Signed { get; set; }
}
public class FRef : NIdentifier<Guid>
{
public FormType Type { get; set; }
public Version Version { get; set; }
public ABC.Common.DInfo Submitted { get; set; }
public ABC.Domain.DInfo Saved { get; set; }
}
As long as I understand you, you want to get the namespace of the object and then decide what to do with it. If this is the case, this should help.

Trouble using all members in a class. Why can I only use the list?

I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.

Categories

Resources