0

I have a class called ModelBase:

public abstract class ModelBase : ViewModelBase
{
    public ModelBase 
    {
        ProcessObjects.Instance.AddProcessObject(name, this);
    }
    public abstract void Dispose();
    public String Name { get; set; }
    ....
    ....
}

public class SomeModel1: ModelBase
{
    public String customprop1 { get; set; }
}

public class SomeModel2: ModelBase
{
    public String customprop2 { get; set; }
}

I keep a list of all object instances in an application singleton. Now somewhere else in the application I want to retrieve customprop2 from SomeModel2 using this singleton list. I can retrieve the object as modelbase object and cast it:

SomeMethod()
{
    if(_obj.Name == "SomeModel2"){
        var _obj = obj as SomeModel2;
        var _customProp2 = obj.customprop2 ;
    }
}

But ideally i want to just try to retrieve the value straight from the object instance by knowing it is there in the parent of the basemodel.

try{
   //Some code to automatically cast the object as parent.
   var _customProp2 = _obj.customProp2;
}catch{
   //Notify user that his request failed
}

The reason for this is that the user can write into a textbox and start a logging function for that particular property.

1 Answer 1

1

Can you create a virtual property/method in the base class and override it in your derived class. This way you can retrieve the values from the object with base class reference, whenever you want.

Sign up to request clarification or add additional context in comments.

1 Comment

In my Base Class I have: public abstract float getOutput();.. Each Parent must implement this method. But this is not really what i want to do for each property. Was hoping for a way to make it less type strong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.