无法将类型ResponseBase隐式转换为Response

时间:2016-04-22 22:07:08

标签: c# asp.net visual-studio-2015 .net-4.6.1

Response response = new Response();

这是显示错误的行'无法将类型ResponseBase隐式转换为响应'

response = await BaseFlow.Invoke(jsonRawRequest, response);

......它正在呼唤:

public static async Task<ResponseBase> Invoke(JToken jsonRawRequest, ResponseBase responseBase)
        {...}

...这是响应类:

class Response : ResponseBase
    {
    // empty
    }

...和 ResponseBase

public class ResponseBase
    {...}

为什么会这样?不应该说'不能隐式转换类型响应到ResponseBase'而不是其他方式arround,因为我试图将一类Response传递给接受ResponseBase的方法?以及如何解决它?

我浏览过互联网并声称我可以将派生类传递给接受基类的方法。

1 个答案:

答案 0 :(得分:3)

当您尝试将<?php /* Plugin Name: Analytics Plugin URI: https://chriscather.wordpress.com Description: Plugin for adding Google Analytics to website Author: C. Cather Version: 1.0 Author URI: https://chriscather.wordpress.com */ add_action('admin_menu', 'cc_analytics_menu'); function cc_analytics_menu() { add_menu_page('CC Analytics', 'CC Analytics', 'administrator', 'cc_analytics_settings_page', 'cc_analytics_menu', plugins_url('analytics/images/analytics.png')); } add_action( 'admin_init', 'cc_analytics_settings' ); function cc_analytics_settings() { register_setting( 'cc_analytics_settings_group', 'accountant_name' ); } function cc_analytics_settings_page(){?> <div class="wrap"> <h2>Staff Details</h2> <form method="post" action="options.php"> <?php settings_fields( 'cc_analytics_settings_group' ); ?> <?php do_settings_sections( 'cc_analytics_settings_group' ); ?> <table class="form-table"> <tr valign="top"> <th scope="row">Accountant Name</th> <td><input type="text" name="accountant_name" value="<?php echo esc_attr( get_option('accountant_name') ); ?>" /></td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php } add_action( 'wp_head', 'cc_analytics' ); function cc_analytics() { ?> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', "<?php cc_analytics_settings_group('accountant_name') ?>", 'auto'); ga('send', 'pageview'); </script> <?php } 作为参数传递时,问题并非如此。

当您尝试将responseInvoke类型)的结果分配给ResponseBase变量(response类型)时出现错误。

要解决此问题,只需将Response类型更改为response

即可
ResponseBase

或使用新的ResponseBase response = new Response(); 变量。