【问题标题】:How do I make this to where I can call it through a DLL?我如何使它成为我可以通过 DLL 调用它的地方?
【发布时间】:2011-09-09 18:01:34
【问题描述】:

我正在创建一个类库,我可以在以后的项目中导入 DLL,然后调用下面的代码向 Twitter 用户发送 DirectMessage。如果我将代码直接包含在 Form1 类中,它会起作用,但我不希望我能够在 VB 中执行类似的操作:

Dim dm as New SendDM
dm.SendDirectMessage(user, message) 

然后就可以了。有任何想法吗?我对 C# 不太熟悉,所以我应该把它公开吗?把它放在它自己的类文件中,比如 SendDM.cs 还是什么?

        private void SendDM(string user, string message)
    {
        if (message.Length == 0)
        {
            MessageBox.Show("Your tweet must be at least 1 character long!");
            return;
        }

        try
        {
            // URL-encode the tweet...
            string tweet = HttpUtility.UrlEncode(message);

            // And send it off...
            string xml = _oAuth.oAuthWebRequest(
                oAuthTwitter.Method.POST,
                 "http://api.twitter.com/1/direct_messages/new.xml",
                "?user=" + user + "&text=" + message);
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred while posting your tweet:\n\n" + ex.Message);
            return;
        }

       message = String.Empty;
    }

【问题讨论】:

    标签: c# class twitter methods


    【解决方案1】:

    是的,你可以。创建一个“类库”项目。插入你的代码并编译它。

    【讨论】:

    • 别忘了让你的方法和你的类公开。
    • 而且我更愿意将该方法设为静态!
    【解决方案2】:

    从类库中,您不应该使用MessageBoxe 来通知异常或验证输入参数,只需从catch 中抛出异常而不是返回并在输入参数未验证时抛出ArgumentOutOfRangeException

    也就是说,如果您希望使用上面显示的 VB 代码发送消息,您应该有一个公共类 SendDM 和一个公共方法 SendDirectMessage

    另外,您不需要在 SendDirectMessage 方法结束时将消息重置为 String.Empty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多