简体   繁体   English

我可以在对象的 New() 构造函数中声明一个委托吗? 还是带初始化参数?

[英]Can I declare a delegate in an Object's New() constructor? Or with initialization parameters?

I'm trying to declare a delegate inline with my data in C# and that doesn't seem to compile.我试图在 C# 中声明一个与我的数据内联的委托,但这似乎无法编译。 I'm hoping someone can help me (re)structure either my thoughts or my code.我希望有人可以帮助我(重新)构建我的想法或代码。

My goal is to create different player types in an online game.我的目标是在在线游戏中创建不同的玩家类型。 Each player type has a strategy they employ.每种玩家类型都有他们采用的策略。 This strategy is employed by calling an inline async function that might call a HTTP REST resource as part of the IQueryable evaluation.此策略通过调用内联异步 function 来使用,该资源可能会调用 HTTP REST 资源作为 IQueryable 评估的一部分。

Question:问题:

  • What is a maintainable way to configure the code required within the EvaluateTurn() delegate for each of the AIPlayer types?为每个AIPlayer类型配置EvaluateTurn()委托中所需的代码的可维护方式是什么?

More player types will be added, and the IQueryable to be a deferred execution Linq Query against CosmosDB, AzureTable, SQL, in-memory array etc.将添加更多播放器类型,并将IQueryable延迟执行 Linq 查询 CosmosDB、AzureTable、SQL、内存数组等。

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }

    public delegate int EvaluateTurn(IQueryable<GameplayRound> playHistory);
}


public List<AIPlayer> CreateDefaultPersonas()
{
    var ret = new List<AIPlayer>();

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "CopyCat" ,
        Description = "Hello! I start with Cooperate, and afterwards, I just copy whatever you did in the last round. Meow",
    });

    ret.Add(new AIPlayer() {
        PlayerPersonaName = "Grudger" ,
         Description = "Listen, pardner. I'll start cooperatin', and keep cooperatin', but if y'all ever cheat me, I'LL CHEAT YOU BACK 'TIL THE END OF TARNATION."
    });

    ret.Add(new AIPlayer() {
        PlayerPersonaName = "AlwaysCheat",
        Description = "the strong shall eat the weak"
    });

   ret.Add(new AIPlayer() {
       PlayerPersonaName = "AlwaysCooperate",
       Description = "Let's be best friends! <3"
   });

   ret.Add(new AIPlayer() {
       PlayerPersonaName = "Detective",
       Description = "First: I analyze you. I start: Cooperate, Cheat, Cooperate, Cooperate. If you cheat back, I'll act like Copycat. If you never cheat back, I'll act like Always Cheat, to exploit you. Elementary, my dear Watson."
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "CopyKitten",
        Description = "Hello! I'm like Copycat, except I Cheat back only after you Cheat me twice in a row. After all, the first one could be a mistake! Purrrrr",
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "Simpleton",
        Description = "hi i try start cooperate. if you cooperate back, i do same thing as last move, even if it mistake. if you cheat back, i do opposite thing as last move, even if it mistake.",
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "Random",
        Description = "Monkey robot! Ninja pizza tacos! lol i'm so random (Just plays Cheat or Cooperate randomly with a 50 / 50 chance)",
    });
   return ret;
}

A delegate is a type, you need to declare a property of that type:委托是一种类型,您需要声明该类型的属性:

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }
    public EvaluateTurn Evaluation { get; set; }

    public delegate int EvaluateTurn(IQueryable<GameplayRound> playHistory);
}

Then:然后:

new AIPlayer()
{
    PlayerPersonaName = "CopyCat" ,
    Description = "Hello! I start with Cooperate, and afterwards, I just copy whatever you did in the last round. Meow",
    Evaluation = playHistory => 1
};

Or you could just use Func for brevity:或者,您也可以使用Func为简洁起见:

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }
    public Func<IQueryable<GameplayRound>, int> Evaluation { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在接口中使用默认参数声明委托给函数? - How can I declare delegate to a function with default parameters in an interface? 是否可以声明没有参数的泛型委托? - Is it possible to declare generic delegate with no parameters? 如何声明一个新的运算符“!??”(测试对象不为null) - How can I declare a new operator “!??” (test object is not null) 我可以将构造函数参数传递给Unity的Resolve()方法吗? - Can I pass constructor parameters to Unity's Resolve() method? 使用Ninject使用构造函数参数实例化新对象 - Instantiating a new object with Constructor parameters using Ninject 我可以使用对象初始化语法在构造函数运行之前为属性赋值吗? - Can I use Object Initialization Syntax to assign values to properties before the constructor runs? 如何将我的数组属性包含到类构造函数中并能够通过对象初始化来访问它? - how can i include my array property into class constructor and be able to access it via object initialization? 基于构造函数的对象初始化 - Constructor based object initialization 我可以使用lambda语法忽略委托参数吗? - Can I ignore delegate parameters with lambda syntax? 为什么我不能在功能级别声明代理? - Why I can`t declare a Delegate at function level?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM