在不参考对象的情况下调用一个函数

问题描述:

在C#中Noob在这里,所以我遇到了一个问题,试图用系统事件参数在C#中调用一个函数。我知道问题不在于系统事件参数,但是我应该调用这种类型的函数的正确方式是什么?在不参考对象的情况下调用一个函数

ERROR1:一个对象引用是所必需的非静态字段, 方法,或属性 'ConsoleApplication3.Program.Reject_call(对象, System.EventArgs)'

误差2:一个对象引用是所需的非静态字段, 方法,或属性 'ConsoleApplication3.Program.Accept_call(对象, System.EventArgs)'

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Lync.Model; 
using Microsoft.Lync.Model.Conversation; 
using Microsoft.Lync.Model.Conversation.AudioVideo; 

namespace ConsoleApplication3 
{ 
    class Program 
    { 
    //holds the Lync client instance 
    private static LyncClient _client; 
    //holds the reference to the conversation associated with this window 
    private Conversation conversation; 
    //self participant's AvModality 
    private AVModality avModality; 

    private static void Main(string[] args) 
    { 
     //Obtains the lync client instance 
     _client = LyncClient.GetClient(); 
     consoleInputReadRoutine(); 
     _client.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded; 
     _client.ConversationManager.ConversationRemoved += ConversationManager_ConversationRemoved; 

     Console.ReadLine(); 
    } 

    static void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e) 
    { 
     e.Conversation.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += Program_ModalityStateChanged; 
    } 

    static void Program_ModalityStateChanged(object sender, ModalityStateChangedEventArgs e) 
    { 
     Console.WriteLine("Modality state changed " + String.Format("{0} => {1}", e.OldState, e.NewState)); 
    } 

    static void consoleInputReadRoutine() 
    { 

     bool done = false; 
     while(!done){ 

      string input = Console.ReadLine(); // Get string from user 
      Console.WriteLine("user input {0}\n", input); 

      // Set exit condition. 
      if(String.Compare(input, "exit") == 0){ 
       done = true; 
      } 
      else if (String.Compare(input, "reject") == 0) 
      { 
       //Do a reject call. 
       Reject_call(null, new EventArgs()); 
      } 
      else if(String.Compare(input, "answer") == 0) 
      { 
       //Do a answer call. 
       Accept_call(null, new EventArgs()); 
      } 
     } 
    } 

    // Accepts an incoming call: AvModality.Accept() 
    private void Accept_call(object sender, EventArgs e) 
    { 
     //accepts an incoming invite (syncronous operation) 
     avModality.Accept(); 
    } 

    // Rejects an incoming call: AvModality.Reject() 
    private void Reject_call(object sender, EventArgs e) 
    { 
     //rejects an incoming invite (which will disconnect the call) 
     //the ModalityDisconnectReason may be used to specify a reason to the caller side 
     //the reason may be shown on the Lync client conversation window 
     avModality.Reject(ModalityDisconnectReason.Decline); 
    } 

    static void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e) 
    { 
     //Exit out of program if you wish after conversation is removed. 
    } 
    } 
} 
+0

如果你想调用/访问没有类的实例的函数/字段/属性,那么函数必须是静态的。 – mason 2014-10-28 17:01:41

您正在调用static之一(static void consoleInputReadRoutine)的实例方法(private void Accept_call)。

鉴于这是一个控制台应用程序,您可能只想使Accept_call静态。一般来说,如果这是一个正常的对象,你会想让所有东西都是非静态的。

private static void Accept_call(object sender, EventArgs e) 

private static void Reject_call(object sender, EventArgs e) 

这些函数使用的类成员也需要是静态的。特别是avModality

+1

并使AVModality avModality静态 – Andez 2014-10-28 17:02:57

+0

@Andez谢谢,包括在内。 – BradleyDotNET 2014-10-28 17:03:41

您需要通过new操作员创建Program类的实例,或者将Reject_callAccept_call标记为static