【问题标题】:The type ' ' has no constructors defined C#类型“”没有定义 C# 的构造函数
【发布时间】:2013-06-13 10:30:27
【问题描述】:

我正在尝试从另一个 cs 文件中调用一个方法。我已经创建了该方法所在的类的新实例,但出现此错误:

“SteamKit2.SteamFriends”类型没有定义构造函数

谁能帮我理解为什么会这样?

我试图从中调用此方法的类如下

using System;
using SteamKit2;
using SteamTrade;
using System.Collections.Generic;
using SteamBot;

namespace SteamBot
{
public class HarvesterHandler : UserHandler
{
    public int FriendCount = 0;
    public int FriendToTrade = 0;
    SteamFriends me = new SteamFriends(); 

    public override void OnLoginCompleted()
    {
        FriendCount = me.GetFriendCount();
        if (FriendToTrade < FriendCount)
        {
            Bot.SteamTrade.Trade(me.GetFriendByIndex(FriendToTrade));
            Log.Info("Sending Trade Request to Friend " + FriendToTrade + " of " + FriendCount);
            return;
        }
        while (true)
        {
            Log.Warn("Finished trading");
        }
    }
}
}

引用的类如下(在单独的cs文件中)

using System.Collections.Generic;
using System.Linq;
using System.Text;
using SteamKit2.Internal;

namespace SteamKit2
{
public sealed partial class SteamFriends : ClientMsgHandler
{
    object listLock = new object();
    List<SteamID> friendList;
    List<SteamID> clanList;

    AccountCache cache;


    internal SteamFriends()
    {
        friendList = new List<SteamID>();
        clanList = new List<SteamID>();

        cache = new AccountCache();
    }
    public int GetFriendCount()
    {
        lock ( listLock )
        {
            return friendList.Count;
        }
    }
    public SteamID GetFriendByIndex( int index )
    {
        lock ( listLock )
        {
            if ( index < 0 || index >= friendList.Count )
                return 0;

            return friendList[ index ];
        }
    }
}
}

【问题讨论】:

  • 尝试使构造函数public而不是internal

标签: c# constructor defined steambot


【解决方案1】:

构造函数被定义为内部的。因此它只能从同一个程序集中访问。可能这个构造函数只打算由工厂调用? SteamFriends 真的和 HarvesterHandler 属于同一个项目吗?

【讨论】:

  • 鉴于命名空间的名称,我敢打赌就是这种情况。
  • 哦等等,不,他们不在同一个项目中。相同的解决方案,不同的项目
  • 好的,有问题。内部仅在同一个程序集内可见 -> 同一个项目
  • 可能您在另一个项目中有代码,但您不小心链接到另一个引用的程序集而不是另一个项目?看看你的参考文献
【解决方案2】:

看起来您使用的是SteamBot,对吗?

我不确定这行的目的:

SteamFriends me = new SteamFriends(); 

您在个人处理程序中所需的一切都在Bot 实例中提供。有关SteamFriends 的相关信息,请查看Bot.SteamFriends(有关示例,请参阅SimpleUserHandler

关于您的问题,我可以为您提供替代方案,因为我不确定为什么如果不花更多时间对项目进行故障排除,GetFriendByIndex 无法正常工作。

您可以通过执行以下操作来遍历机器人的整个好友列表(如果我没记错 Valve 系统的限制,最多有 250 个可能的好友):

foreach (Bot.SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
{
    // Check the friend ID (Steam Profile ID - 64bit number) against what who you want to trade
    if (friend.SteamID == "VALUE YOU ARE EXPECTING")
    {
        Bot.SteamTrade.Trade(friend);
    }
}

【讨论】:

    【解决方案3】:

    将 SteamFriends 类中的所有方法设为静态,然后使用类名从类名访问。 你肯定会得到问题的解决方案。 喜欢-:

    public static int GetFriendCount()
        {
            lock ( listLock )
            {
                return friendList.Count;
            }
        }
        public  static SteamID GetFriendByIndex( int index )
        {
            lock ( listLock )
            {
                if ( index < 0 || index >= friendList.Count )
                    return 0;
    
                return friendList[ index ];
            }
        }
    

    并从 HarvesterHandler cs 文件访问。 不要成为类的对象。
    FriendCount = SteamFriends.GetFriendCount();..

    干杯!..

    【讨论】:

      【解决方案4】:

      这是因为无参构造函数的可访问性设置为内部变化:

      internal SteamFriends()
      

      public SteamFriends()
      

      【讨论】:

      • 有问题的答案,SteamFriends 的构造函数可能被定义为internal 是有原因的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2014-02-28
      相关资源
      最近更新 更多