简体   繁体   中英

how get values from object to local variable c#

i have following code please help me how to get values from object

 APIKarho objapi = new APIKarho();
           object obje = objapi.GetBookingFromAPI();

           string ss = obje.booking_id;

在此处输入图片说明

You should cast the object to the class it originally is (what class is the object returned by GetBookingFromAPI() ) before you could access its field/property/method . Example:

public MyClass { // suppose this is the original class of the object returned by GetBookingFromAPI
    public int booking_id; 
}

APIKarho objapi = new APIKarho();
object obje = objapi.GetBookingFromAPI();
string ss = ((MyClass)obje).booking_id; //note the casting to MyClass here

You need to find out what type GetBookingFromAPI() returns, and change the type of obje. Just move your mouse over GetBookingFromAPI().

GetBookingFromAPIType obje = objapi.GetBookingFromAPI();

string ss = obje.booking_id;

If your api returns an object of an unknown type or a type that you cannot cast to you could use the dynamic keyword.

dynamic obj = api.GetBookingFromAPI();
string ss = obj.booking_id;

Note that this works only if booking_id is actually a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM