Jay*_*uzi 1 c# extension-methods dynamic .net-4.0 c#-4.0
我在考虑Regex.Match.Group如何变得动态:
Regex.Match (...).Groups["Foo"]
Run Code Online (Sandbox Code Playgroud)
我想成为:
Regex.Match (...).Groups.Foo
Run Code Online (Sandbox Code Playgroud)
我想过编写一个允许的扩展方法:
Regex.Match (...).Groups().Foo
Run Code Online (Sandbox Code Playgroud)
并尝试以这种方式编写,但这是不允许的(';''静态动态'需要')
public static dynamic DynamicGroups Groups(this Match match)
{
return new DynamicGroups(match.Groups);
}
public class DynamicGroups : DynamicObject
{
private readonly GroupCollection _groups;
public DynamicGroups(GroupCollection groups)
{
this._groups = groups;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Group g = this._groups[binder.Name];
if (g == null)
{
result = null;
return false;
}
else
{
result = g;
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
之前编写的大量其他API dynamic
可能更清晰,以这种方式使用.
你的代码中只有一个小错误,dynamic DynamicGroups
改为justdynamic
public static dynamic Groups(this Match match)
{
return new DynamicGroups(match.Groups);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
377 次 |
最近记录: |