How to return dynamic return types to methods? FROM#

I have a problem with the return type of the method.

The method returns a linq object, which currently returns the type tblAppointment. This method is shown below:

public tblAppointment GetAppointment(int id)
{
    var singleAppointment = (from a in dc.tblAppointments
                                                    where a.appID == id
                                                    select a).SingleOrDefault();
    return singleAppointment;

}

The problem is that tblAppointment is abstract and has many subtypes that inherit it. When I try to return an object that is of type "destinationTypeA" and calls the .GetType () method, it gives me the correct subtype, but when I try to access the properties, it allows me to access the parent properties, If I take the object and pass it to a new subtype object, then it works and allows me to access everything I need, but it seems messy.

var viewSingleAppointment = appointmentRepos.GetAppointment(appointmentId);

Debug.Write(viewSingleAppointment.GetType()); //returns type i want

if (viewSingleAppointment is tblSingleBirthAppointment)
{
    tblSingleBirthAppointment myApp = (tblSingleBirthAppointment)viewSingleAppointment; //need to do this to access TypeA properties for some reason

}

: , select ( 20) , , , , .

+5
6

. A, B, C .., , :

  • A , B, C .. , B C, , A. , , , .

  • , . , :

    A foo = GetA();
    if(foo is B) {
        B bFoo = (B) foo;
        // Do something with foo as a B
    } else if(foo is C) {
        C cFoo = (C) foo;
        // Do something with foo as a C
    } ...
    

    ( , , ):

    A foo = GetA();
    MyEnum enumeratedValue = foo.GetEnumeratedValue();
    switch(enumeratedValue) {
        case MyEnum.B:
            B bFoo = (B) foo;
            // Do something with foo as a B
            break;
        case MyEnum.C:
            C cFoo = (C) foo;
            // Do something with foo as a C
            break;
    }
    

    - :

    A foo = GetA();
    foo.DoSomething();
    

    switch. :

    • .
    • , ; -.
    • switch/case B C, case if . DoSomething() A, .

. :

DoSomething() Form GUI, . :

public class B : A {
    public void DoSomething(MyForm form) {
        form.MyLabel.Text = "I'm a B object!";
    }
}

public class C : A {
    public void DoSomething(MyForm form) {
        form.MyLabel.Text = "I'm a C object!";
    }
}

// elsewhere, in a method of MyForm:

A foo = GetA();
foo.DoSomething(this);

, B C , , , .

+7

, # 4, ... , , , , :

public T GetAppointment<T>(int id) where T : tblAppointment
{
    var singleAppointment = (from a in dc.tblAppointments
                                                    where a.appID == id
                                                    select a).SingleOrDefault();
    return (T) singleAppointment;

}

:

SpecificAppointment app = GetAppointment<SpecificAppointment>(10);

:

var app = GetAppointment<SpecificAppointment>(10);

, .

, ( tblAppointment, ). , , , ...

+7

:

public T GetAppointment<T>(int id) where T : tblAppointment 
{
    var singleAppointment = dc.tblAppointments.SingleOrDefault(a => a.appID == id);
    return (T)singleAppointment;
}

...

+2

.GetType(), . # , . , , tblAppointment, , tblAppointment. tblAppointment - , , , " , , ".

- , . , Smalltalk Javascript, . , - , , - .

: , , , :

class tblAppointment
{
    protected abstract void ProcessAppointment () ;
}

sealed class tblBirthAppointment
{
    protected override void ProcessAppointment ()
    {
        // `this` is guaranteed to be tblBirthAppointment
        // do whatever you need
    }
}

...

// will dispatch on runtime type
appointmentsRepo.GetAppointment (id).ProcessAppointment () ;
+2

:

public tblSingleBirthAppointment GetBirthAppointment(int id)
{
    var singleAppointment = GetAppointment(id);

    if (singleAppointment != null)
    {
        return (tblSingleBirthAppointment)singleAppointment;
    }

    return null;
}

, , BirthAppoint, .

var viewSingleBirthAppointment = appointmentRepos.GetBirthAppointment(appointmentId);
+1

, , , , . :)

, - , , , .

0

All Articles