小编Ale*_*rt.的帖子

AutoFixture - 相似.复杂对象的比较

我试图将2个复杂对象与AutoFixture进行比较,OfLikeness但遗憾的是没有成功.在比较嵌套对象(也是OfLikeness)时,按预期工作,比较主对象结果与错误说明子对象不匹配.我假设问题是Likeness仅将语义比较器应用于主对象,并使用默认的Equal实现来比较嵌套对象,该实现检查引用匹配(可能是我的假设是错误的?).

这可以澄清我想要实现的目标:

public class ComplexMasterObject{
    public ChildFirst child1 {get;set;}
    public ChildSecond child2 {get;set;}
    public string SimpleProp {get;set;}
}

public class ChildFirst {
    public string SomeStringProp1 {get;set;}
    public int  SomeIntProp1 {get;set;}
}

public class ChildSecond {
    public string SomeStringProp1 {get;set;}
    public int  SomeIntProp1 {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

测试:

public void TestLikeness_Success()
{
     var expected = new ComplexMasterObject {
         Child1 = new ChildFirst {
             SomeStringProp1 = "ChildFirst SomeStringProp1",
             SomeIntProp1 = 1
         },
         Child2 = new ChildSecond {
             SomeStringProp1 = "ChildSecond …
Run Code Online (Sandbox Code Playgroud)

c# testing compare autofixture

6
推荐指数
1
解决办法
2309
查看次数

反射方法调用与委托调用的性能

我的目标是编写一个弱类型的 TryParse方法,它基本上支持所有可用的struct类型(int,long,float ...)

public static bool TryParse(Type type, string s, out object obj)
Run Code Online (Sandbox Code Playgroud)

该实现将调用所TryParse提供type参数的方法(如果是int,int.TryPase则将调用type ,并将out值作为对象返回).

我通过反射实现了它,但是有一个主要的性能损失(正如我预期的那样)

反思实施:

public static class ParserHelper
{
    public delegate bool TryParseDl(string str, out object obj);

    private static readonly HashSet<Type> ParsableStructs = new HashSet<Type>
    {
    typeof(int),
    typeof(uint),
    typeof(decimal),
    typeof(short),
    typeof(ushort),
    typeof(double),
    typeof(long),
    typeof(ulong),
    typeof(float),
    typeof(byte),
    typeof(sbyte)
    };

    public static readonly ReadOnlyDictionary<Type, TryParseDl> StructParsers;

    static ParserHelper()
    {
        StructParsers = new ReadOnlyDictionary<Type, TryParseDl>(CreateParsersForStructs());
    }

    /// Creates parsers for …
Run Code Online (Sandbox Code Playgroud)

c# reflection performance delegates

5
推荐指数
1
解决办法
1979
查看次数

正则表达式:用特殊字符替换数字中间的数字

我有很多输入(它的长度可能会有所不同)作为输入。

我需要一个正则表达式,该表达式将保留前3位数字和后3位数字不变,并将它们之间的所有数字替换为某些字符。输出的总长度应保持不变。

例如:

输入123456789123456

输出123xxxxxxxxx456

到目前为止,我已经能够通过使用将输入数字分为3组

^(\d{3})(.*)(\d{3})
Run Code Online (Sandbox Code Playgroud)

第二组是需要替换的组,因此它将类似于

$1 {Here goes the replacement of the 2 group} $3
Run Code Online (Sandbox Code Playgroud)

我正在努力更换:

Regex r = new Regex("^(\d{3})(.*)(\d{3})");
r.Replace(input,"$1 {Here goes the replacement of the 2 group} $3")
Run Code Online (Sandbox Code Playgroud)

我应该如何在这里写2组的替代品?

提前致谢。

c# regex

1
推荐指数
1
解决办法
582
查看次数

标签 统计

c# ×3

autofixture ×1

compare ×1

delegates ×1

performance ×1

reflection ×1

regex ×1

testing ×1