2017-03-15 5 views
-1

MyFirstBot 프로젝트의 Plugins 폴더에서 TwitchAPIexample이라는 파일입니다. 클래스와 코드는 다음과 같습니다 :C# Beginner - 다른 클래스의 클래스 사용

using System.Net; 
using System.IO; 
using Newtonsoft.Json; 

namespace MyFirstBot.Plugins 
{ 
    public class TwitchAPIexample 
    { 

     private const string url = "https://api.twitch.tv/kraken/streams/<channel>"; 

     public bool isTwitchLive; 

     private static void BuildConnect() 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

      request.Method = "Get"; 
      request.Timeout = 12000; 
      request.ContentType = "application/json"; 
      request.Headers.Add("authorization", "<token>"); 

      using (System.IO.Stream s = request.GetResponse().GetResponseStream()) 
      { 
       using (StreamReader sr = new System.IO.StreamReader(s)) 
       { 
        var jsonResponse = sr.ReadToEnd(); 
        RootObject r = JsonConvert.DeserializeObject<RootObject>(jsonResponse); 
       } 
      } 
     } 


     public class Preview 
     { 
      public string small { get; set; } 
      public string medium { get; set; } 
      public string large { get; set; } 
      public string template { get; set; } 
     } 

     public class Channel 
     { 
      public bool mature { get; set; } 
      public string status { get; set; } 
      public string broadcaster_language { get; set; } 
      public string display_name { get; set; } 
      public string game { get; set; } 
      public string language { get; set; } 
      public int _id { get; set; } 
      public string name { get; set; } 
      public string created_at { get; set; } 
      public string updated_at { get; set; } 
      public bool partner { get; set; } 
      public string logo { get; set; } 
      public string video_banner { get; set; } 
      public string profile_banner { get; set; } 
      public object profile_banner_background_color { get; set; } 
      public string url { get; set; } 
      public int views { get; set; } 
      public int followers { get; set; } 
     } 

     public class Stream 
     { 
      public long _id { get; set; } 
      public string game { get; set; } 
      public int viewers { get; set; } 
      public int video_height { get; set; } 
      public int average_fps { get; set; } 
      public int delay { get; set; } 
      public string created_at { get; set; } 
      public bool is_playlist { get; set; } 
      public Preview preview { get; set; } 
      public Channel channel { get; set; } 
     } 

     public class RootObject 
     { 
      public Stream stream { get; set; } 
     } 


    } 

} 

는 내가 뭘해야 MyFirstBot 프로젝트 파일에서 다른 파일에 네임 스페이스 MyfirstBot.Plugins의 클래스를 사용합니다. 나는 가지고있다 :

using namespace MyFirstBot.Plugins 

그러나 나는 RootObject를 어떻게 사용하는지 모르겠다. 내가 사용 시도 :

TwitchAPIexample.stream TwitchLive = new TwitchAPIexample.stream() 

하지만 난 정말 JSON에있는 다른 문자열을 확인하기 위해 거기에서 이동하는 방법을 잘 모릅니다의 TwitchAPIexample 클래스의 모든 것을 조작 할 수 얼마나 기본적으로, 문자열로는 동일하게 설정.

다시 C# Noob이므로 나를 위해 작성하지 않아도되지만, 설명 할 수 있거나 좋은 리소스로 나를 때릴 수 있다면. 나는 봤 거든 여전히 혼란스러워. OOP는 내 강한 옷이 아니야.

이것은 지금까지 내가 입수했습니다과 같습니다

namespace MyFirstBot 
{ 
    public class DiscordBot 
    { 
     DiscordClient client; 
     CommandService commands; 
     TwitchClient TwitchClient; 
     TwitchAPIexample.Stream TwitchLive = new TwitchAPIexample.Stream(); 
     public DiscordBot() 
     { 
      if(TwitchLive.equals(null)) 
      { 
       //stream is offline 
      } 
     } 
    } 
} 

나는 이것이 가장 좋은 방법입니다 확실하지 않다.

+0

귀하의'TwitchAPIexample' 클래스하지 않는 스트림에 액세스 할 수 twitchLive.Root를 사용하여 루트 물마루 루트 개체에 액세스하는 다음 'Stream'이라는 메소드를 가지고 있기 때문에 코드의이 라인은'새로운 TwitchAPIexample.Stream()'과 같은 의미가 없습니다. 어떤 스트림에 액세스하려고합니까? –

+1

@IsaacKleinman -'Stream'은'TwitchAPIexample'의 중첩 클래스입니다. 이 예제의 맥락에서, 사용법은 'Stream'의 새로운 인스턴스를 새로 만들 때 의미가 있습니다. –

+0

'BuildObject'메소드를 변경하여'rootObject'를 리턴 한 다음,'DiscordBot' 클래스에서'BuildConnect()'를 호출하여 json 응답의 세부 사항을 얻으려고합니다. . –

답변

0

나를 위해 아키텍처를 약간 변경해야하는 것처럼 보입니다. 정적 메서드가 필요하지 않으며 RootObject에 액세스 할 수있는 속성 트로프를 만들어야합니다. 그리고 그 수업들을 중첩 할 필요는 없습니다. 이제

public class TwitchAPIexample 
{ 

    private const string url = "https://api.twitch.tv/kraken/streams/<channel>"; 

    public bool IsTwitchLive { get; set; } 
    public RootObject Root { get; set; } 

    public TwitchAPIexample() 
    { 
     BuildConnect(); 
    } 

    private void BuildConnect() 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     request.Method = "Get"; 
     request.Timeout = 12000; 
     request.ContentType = "application/json"; 
     request.Headers.Add("authorization", "<token>"); 

     using (System.IO.Stream s = request.GetResponse().GetResponseStream()) 
     { 
      using (StreamReader sr = new System.IO.StreamReader(s)) 
      { 
       var jsonResponse = sr.ReadToEnd(); 
       this.Root = JsonConvert.DeserializeObject<RootObject>(jsonResponse); 
      } 
     } 
    } 
} 

public class Preview 
{ 
    public string small { get; set; } 
    public string medium { get; set; } 
    public string large { get; set; } 
    public string template { get; set; } 
} 

public class Channel 
{ 
    public bool mature { get; set; } 
    public string status { get; set; } 
    public string broadcaster_language { get; set; } 
    public string display_name { get; set; } 
    public string game { get; set; } 
    public string language { get; set; } 
    public int _id { get; set; } 
    public string name { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public bool partner { get; set; } 
    public string logo { get; set; } 
    public string video_banner { get; set; } 
    public string profile_banner { get; set; } 
    public object profile_banner_background_color { get; set; } 
    public string url { get; set; } 
    public int views { get; set; } 
    public int followers { get; set; } 
} 

public class Stream 
{ 
    public long _id { get; set; } 
    public string game { get; set; } 
    public int viewers { get; set; } 
    public int video_height { get; set; } 
    public int average_fps { get; set; } 
    public int delay { get; set; } 
    public string created_at { get; set; } 
    public bool is_playlist { get; set; } 
    public Preview preview { get; set; } 
    public Channel channel { get; set; } 
} 

public class RootObject 
{ 
    public Stream stream { get; set; } 
} 

당신이 할 수있는

namespace MyFirstBot 
{ 
    public class DiscordBot 
    { 
     DiscordClient client; 
     CommandService commands; 
     TwitchClient TwitchClient; 
     TwitchAPIexample twitchLive = new TwitchAPIexample(); 
     public DiscordBot() 
     { 
      if(twitchLive.Root == null || twitchLive.Root.Stream == null) 
      { 
       //stream is offline 
      } 
     } 
    } 
} 

그래서 당신은 당신이 twitchLive.Root.Stream