tags:

views:

185

answers:

3
+7  A: 

If you're doing what I think you are you would have to change your constructor slightly:

public Request(Parameters parameters) {
  this.Parameters = parameters;
}

and then you can do this:

public class SpecificRequest : Request {
  public class SpecificRequestParameters : Request.Parameters {
  }
  public SpecificRequest() : base(new SpecificRequestParameters()) {
    //More stuff here
  }
}

What's the specific problem that you're trying to address? What you're doing here seems fairly awkward and overly complicated.

George Mauer
Implementing JSON communication. I wish this could be done using arrays. Grrrrr .... I hate it - I have about 20 classes for each request and 20 more for corresponding responses :(
Nick Brooks
You may consider using something like JSon.Net ?
flq
I am using JSON.NET - I am serializing JSON objects into classes
Nick Brooks
If this is JSON why even bother having a strongly typed Parameters object at all? Just stick all the parameters in an IDictionary. Parameters is just a data-transfer-object anyways, its not going to have any behavior.
George Mauer
+1  A: 

Why do you embed the Parameters class inside the Request class? Instead, let Request have an instance of parameters and just declare the class somewhere else.

Answer for your question 1: Inherit from Request and the original request constructor will always be called.

Question 2: Subclasses cannot add members to parameter classes (except with reflection, but you dont want to walk that path). Best you can do is inherit from parameter and have the InheritedRequest use the InheritedParameter class. Note that in that case you cannot override the properties and properties. You will have to have an additional property in InheritedRequest called AdditionalParameters of type InheritedParameters.

Henri
+1  A: 

Constructors can be chained with the syntax

ctor() : this()

or

ctor() : base()

in there you can pass parameters along etc.

Make the Parameters field generic

abstract class Request<T> where T : Parameters {
  T Parameters;
}

class Specialized : Request<SpecialParameters> {
}

where SpecialParameters inherits from Parameters.

flq