我有一个类型的现有表达Expression<Func<T, object>>; 它包含像cust => cust.Name.
我还有一个带有类型字段的父类T.我需要一个接受上面作为参数的方法,并生成一个新的表达式,将父类(TModel)作为参数.这将用作MVC方法的表达式参数.
因此,cust => cust.Name成为parent => parent.Customer.Name.
同样,cust => cust.Address.State成为parent => parent.Customer.Address.State.
这是我的初始版本:
//note: the FieldDefinition object contains the first expression
//described above, plus the MemberInfo object for the property/field
//in question
public Expression<Func<TModel, object>> ExpressionFromField<TModel>(FieldDefinition<T> field)
where TModel: BaseModel<T>
{
var param = Expression.Parameter(typeof(TModel), "t");
//Note in the next line "nameof(SelectedItem)". This is a reference
//to the property in …Run Code Online (Sandbox Code Playgroud) 我不擅长统计数据,所以我试图在C#中解决一个简单的问题.问题是:"特定球队有65%的机会赢得一场对阵另一支球队的比赛.他们赢得5胜一筹的概率是多少?"
我想看看这个概率与集合中游戏数量之间的关系.Bo3如何与Bo5相比,等等?
我通过创建Set和Game对象以及运行迭代来完成此操作.胜利决定是使用以下代码完成的:
Won = rnd.Next(1, 100) <= winChance;
Run Code Online (Sandbox Code Playgroud)
rnd正如您所料,是一个静态System.Random对象.
这是我的Set目标代码:
public class Set
{
public int NumberOfGames { get; private set; }
public List<Game> Games { get; private set; }
public Set(int numberOfGames, int winChancePct)
{
NumberOfGames = numberOfGames;
GamesNeededToWin = Convert.ToInt32(Math.Ceiling(NumberOfGames / 2m));
Games = Enumerable.Range(1, numberOfGames)
.Select(i => new Game(winChancePct))
.ToList();
}
public int GamesNeededToWin { get; private set; }
public bool WonSet => Games.Count(g => g.Won) >= …Run Code Online (Sandbox Code Playgroud) 我只知道我是个白痴,所以有人请告诉我怎么做.
设置很简单:
创建三个项目命名溶液(.NET框架类库)InherTest,InherTest.Base以及InherTest.Base.Inherited
在InherTest.Base,添加以下类:
namespace InherTest.Base {public abstract class BaseClass {internal abstract string MEMBER_1 {get; }}}
将完全相同的代码复制到InherTest,包括命名空间.
在InherTest.Base.Inherited,添加以下类:
namespace InherTest.Base.Inherited {public class Inherited:BaseClass {internal override string MEMBER_1 {get; }}}
在添加项目引用InherTest.Base.Inherited到InherTest.Base.注意错误("抽象成员未实现"和"没有找到合适的成员覆盖").
删除该引用并将其替换为InherTest.观察到继承也失败了.
他们为什么不工作?
编辑:最初我说过一次测试失败而另一次测试失败; 但是,在上述场景中,两个继承尝试都失败了.
我在C#中完成了大部分开发工作,我只是在学习F#.这是我想用C#做的事情:
string AddChars(char char1, char char2) => char1.ToString() + char2.ToString();
Run Code Online (Sandbox Code Playgroud)
编辑:添加ToString()方法到C#示例.
我想在F#中编写相同的方法,除了这个我不知道怎么做:
let addChars char1 char2 = Char.ToString(char1) + Char.ToString(char2)
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些chars 连接到一个string没有先转换成strings的?
边注:
我也考虑过制作一个char阵列并将其转换为a string,但这看起来同样浪费.
let addChars (char1:char) (char2: char) = string([|char1; char2|])
Run Code Online (Sandbox Code Playgroud)