如何在不同方法中使用方法内变量的数据(同一类中的两个方法)?

时间:2016-02-14 17:07:52

标签: c# .net object methods oauth-2.0

我想从Second方法中的First方法重用 OAuth2Parameters参数。这是代码:

Starting JS server...
Starting the packager in a new window is not supported on Windows yet.
Please start it manually using 'react-native start'.
We believe the best Windows support will come from a community of people
using React Native on Windows on a daily basis.
Would you be up for sending a pull request?
Building and installing the app on the device (cd android && gradlew.bat install
Debug)...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
   > Could not resolve com.facebook.react:react-native:0.19.+.
     Required by:
         sess:app:unspecified
      > Could not resolve com.facebook.react:react-native:0.19.+.
         > Failed to list versions for com.facebook.react:react-native.
            > Unable to load Maven meta-data from https://jcenter.bintray.com/co
m/facebook/react/react-native/maven-metadata.xml.
               > Could not GET 'https://jcenter.bintray.com/com/facebook/react/r
eact-native/maven-metadata.xml'.
                  > Connection to https://jcenter.bintray.com refused

* Try:
Run with —stacktrace option to get the stack trace. Run with —info or —debug
option to get more log output.

BUILD FAILED

Total time: 48.737 secs
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html

我该如何实现?

2 个答案:

答案 0 :(得分:1)

如果第一个方法始终在第二个方法之前调用,则可以为该类声明成员字段。

public class SomeController : ApiController
{
    OAuth2Parameters parameters;

    public string First()
    {
        string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);

        return authorizationUrl;
    }

    public void Second(string someAccessCode)
    {
        // I want to reuse the above OAuth2Parameters parameters here:
        parameters.AccessCode = someAccessCode;

        OAuthUtil.GetAccessToken(parameters);
        string accessToken = parameters.AccessToken;
    }
}

如果您不确定,请使用具有支持字段的属性。

public class SomeController : ApiController
{
    OAuth2Parameters _parameters;

    private OAuthParameters {
      get {
        if (_parameters == null) {
          _parameters = new OAuth2Parameters();

          _parameters .ClientId = "someClientID";
          _parameters .ClientSecret = "someClientSecret";
          _parameters .RedirectUri = "someRedirectUri";
          _parameters .Scope = "someScope";
        }

        return _parameters;
      }
    }

    public string First()
    {
        string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(OAuthParameters);

        return authorizationUrl;
    }

    public void Second(string someAccessCode)
    {
        // I want to reuse the above OAuth2Parameters parameters here:
        parameters.AccessCode = someAccessCode;

        OAuthUtil.GetAccessToken(OAuthParameters);
        string accessToken = OAuthParameters.AccessToken;
    }
}

答案 1 :(得分:0)

试试这个..Declare变量高于First(),以便它可用于两个方法

 <input type="file" name="file" class="form-control" placeholder="Upload Your CV"  /><?php echo $form->error("file"); ?>