创建实现DynamicObject的类
public class Test : DynamicObject
{
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder.Name == ("Posts"))
{
result = "property accessed was 'Posts'";
return true;
}
return base.TryGetMember(binder, out result);
}
}
我可以打电话
dynamic test = new Test();
var result = test.Posts;
result
的值是“dynamic test = new Test();
var result = test.Posts;“
没关系。
我想知道的是,当调用TryGetMember时,可以获得链接值。
所以,如果我打电话:
dynamic test = new Test();
var result = test.Posts.Load(123);
然后我可以做类似的事情:
if (binder.Name == ("Posts"))
{
if (... == "Load")
result = this.Load<Post>(... 123);
return true;
}
这样的事情可能吗?我无法想办法做到这一点。
到目前为止,我有:
class Program
{
static void Main(string[] args)
{
dynamic test = new Test();
dynamic result = test.Posts.Load(123);
Console.WriteLine(result.Name);
dynamic result2 = test.Posts.Load(909);
Console.WriteLine(result2.Name);
Console.ReadKey();
}
}
public class Test : DynamicObject
{
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (binder.Name == ("Posts"))
{
result = new ChainBuilder(this, "Post");
return true;
}
return base.TryGetMember(binder, out result);
}
public T Load<T>(int id) where T : Post, new()
{
if (id == 123)
return new T {Id = 123, Name = "Bananas"};
return new T {Id = 0, Name = "Others"};
}
private class ChainBuilder : DynamicObject
{
public dynamic OriginalObject { get; set; }
public string PropertyInvoked { get; set; }
public ChainBuilder(DynamicObject originalObject, string propertyInvoked)
{
OriginalObject = originalObject;
PropertyInvoked = propertyInvoked;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (binder.Name == "Load")
{
result = OriginalObject.Load<Post>((int)args[0]);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
}
}
public class Post
{
public int Id { get; set; }
public string Name { get; set; }
}
感谢Bartosz。
但看起来它基本上就是Marc所提供的。
给我一个很好的起点!对于任何其他建议,我现在暂时保持开放状态。
此问题已导致
不是一个真正的项目,只是原型设计,但实现了我们想要的目标。
答案 0 :(得分:3)
评估的每个步骤都是分开的;它不评估.Posts.Load(123)
- 它评估.Posts
,然后单独评估.Load(123)
,所以不:你不能一步完成。诀窍是自己编写值,例如:
using System;
using System.Dynamic;
using System.Text;
static class Program {
static void Main() {
dynamic test = new Test();
var result = test.Posts.Foo.Bar(123, "abc");
Console.WriteLine(result);
}
}
public class Test : DynamicObject
{
public override bool TryGetMember(GetMemberBinder binder,
out object result)
{
result = new MemberAccessWrapper("member accessed was " + binder.Name);
return true;
}
private class MemberAccessWrapper : DynamicObject
{
private readonly string message;
public override bool TryInvoke(InvokeBinder binder, object[] args,
out object result)
{
StringBuilder builder = new StringBuilder(message).Append("(");
for(int i = 0 ; i < args.Length ; i++) {
if(i!=0)builder.Append(", ");
if (args[i] == null) {
builder.Append("null");
} else if (args[i] is string) {
builder.Append("@\"").Append(((string)args[i])
.Replace("\"", "\"\"")).Append("\"");
} else {
builder.Append(args[i]);
}
}
builder.Append(")");
result = new MemberAccessWrapper(builder.ToString());
return true;
}
public MemberAccessWrapper(string message)
{
this.message = message;
}
public override string ToString()
{
return message;
}
public override bool TryGetMember(GetMemberBinder binder,
out object result)
{
result = new MemberAccessWrapper(message + "." + binder.Name);
return true;
}
}
}
答案 1 :(得分:1)
也许你可以通过实现基本动态接口而不是'DynamicObject'来实现这一点,但最简单的方法是动态调用'Posts'来返回另一个将处理'Load'方法的DynamicObject。 / p>