我需要将类类型转换为对象类型,需要将c1转换为object2

时间:2015-08-04 14:46:03

标签: java

第一个方法返回一个类类型,第二个方法返回一个对象类型。我需要将class1转换为object2类型

function get_https_content($url=NULL,$method="GET"){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0');
    curl_setopt($ch, CURLOPT_URL,$url);
    return curl_exec($ch);
}

function ig_count($username) {
    return json_decode(get_https_content("https://api.instagram.com/v1/users/1460891826/?client_id=ea69458ef6a34f13949b99e84d79ccf2"))->data->counts->followed_by;
}

2 个答案:

答案 0 :(得分:0)

您是否在Class类中寻找“强制转换”方法?

Class<T> c1 = m.getReturnType();

T casted = c1.cast(object2);

答案 1 :(得分:-1)

有两种方法可以做到这一点。

1)使用c1.newInstance()获取对象(抛出已检查的异常以进行处理),然后将返回的对象强制转换为所需的对象

2)使用c1.cast(object2),如果无法创建实例或对象不属于该类,则ClassCastException会抛出public class PriceValue { public decimal Value { get; set; } ... } ... Mapper.CreateMap<PriceValue, decimal>() .ConvertUsing(src => src.Value); 。实例(首选方法)