1

I'm pretty new to C# and it may well be that my approach is completely inadequate... I'm trying to build a class which is a container for another type which may be extended in a derived class. Basically, my current code looks something like this:

public class BaseFormFields
{
   public String baseFieldName1 {get; set;}

}
public abstract class BaseOrder<F> where F : BaseFormFields
{
   public String orderProperty1 {get; set;}
   public F formFields { get; set; }
}

in a derived class

public class SpecificFormFields : BaseFormFields
{
   public String specificFieldNam1 {get; set;}
}
public class SpecificOrder : BaseOrder<SpecificFormFields>
{
}

while this basically works, how would I need to construct a method in either class BaseFormFields or SpecificFormFields to access members from BaseOrder? I tried:

public class BaseFormFields
{
    public string BaseFieldName1 { get; set; }
    public Object _order;
    public BaseOrder<BaseFormFields> order
    {
        get
        {
            return (BaseOrder<BaseFormFields>)_order;
        }
    }
}
public abstract class BaseOrder<F> where F : BaseFormFields
{
    public String orderProperty1 { get; set; }
    private F _formFields;
    public F formFields
    {
        get
        {
            return _formFields;
        }
        set
        {
            value._order = this;
            _formFields = value;

        }
    }
}

public class SpecificFormFields : BaseFormFields
{
    public String SpecificFieldName1 { get; set; }
    public String SpecificFieldName2
    {
        get
        {
            return order.orderProperty1;
        }
    }
}

public class SpecificOrder : BaseOrder<SpecificFormFields>
{

}

which compiles alright but gives me an InvalidCastException when I try to:

SpecificOrder specificOrder = new SpecificOrder();
specificOrder.orderProperty1 = "Test";
specificOrder.formFields = new SpecificFormFields();
String s = specificOrder.formFields.SpecificFieldName2;

Any help is appreciated...

ub_coding
  • 139
  • 5
  • 1
    This might be one of those "If you have a hammer everything looks like a nail" situations. Maybe you do not need to use generics here. Try to create a solution that just uses classes with inheritance or interface implementation where needed (maybe form field). If you notice there is a type that could be abstracted into a generic because you need to know the type specifics at design/compile time then generics *might* be a good implementation. – Igor Sep 25 '18 at 09:26
  • Can you please show us the constructor for `BaseOrder`? How are you populating the `formFields` property? – Flater Sep 25 '18 at 09:26
  • @PatrickHofman: The duplicate you provided is not what this question is about. This question should not have been closed. – Flater Sep 25 '18 at 09:28
  • And why not? That comment is only useful with some explanation. @Flater – Patrick Hofman Sep 25 '18 at 09:29
  • @PatrickHofman: Because this question is not centered on downcasting generic types. As per OP's phrasing: `how would I need to construct a method in either class BaseFormFields or SpecificFormFields to access members from BaseOrder?` He is merely trying to reference/access his `BaseOrder` class from inside the `FormFields` class. The generics are actually tangential to the question at hand (more so than OP realizes - I suspect) – Flater Sep 25 '18 at 09:30
  • @Flater: formFields are populated by deserializing a web service request. That's in fact why I tried to use generics as the webservice client in question tries to PUT objects from different derived classes at the same endpoint. My idea thus was to have different actions in my MVC controller to let the router decide which type was PUT. Something like ActionResult PutSpecificOrder1([FromBody] SpecificOrderType1 order) and then PutSpecificOrder2([FromBody] SpecificOrderType2 order) – ub_coding Sep 25 '18 at 10:05
  • @ub_coding: The `FormFields` object may be instantiated from the web request, but how are you assigning this object to the `formFields` _property_ of the `BaseOrder` class? Are you deserializing the entire `Order` from the webrequest, including its underlying `formFields` property? – Flater Sep 25 '18 at 10:13
  • Sorry, I had been out of office for a few days... Yes, it's the entire order object which is deserialized from the web service request. Point of the matter is that orders of different type do share "meta data" described in the properties which are part of the base order object and differ in the layout of form fields supplied in the "form" property. – ub_coding Oct 04 '18 at 09:10

0 Answers0