使用 ValueInjecter 从 ExpandoObject 注入

发布于 2024-11-26 23:00:53 字数 423 浏览 3 评论 0原文

我正在使用 ValueInjecter 进行对象映射,并尝试从 ExpandoObject 注入。我找到了一个从动态注入的例子。

   public class Ac
    {
        public string Aa { get; set; }
    }

    [Test]
    public void Aa()
    {
        var o = new { Aa = "aa" };
        dynamic d = o;
        var a = new Ac{ Aa = "bb" };
        a.InjectFrom((object)d);
        Assert.AreEqual(o.Aa, a.Aa);
    }

但我还没有成功地让它与 ExpandoObject 一起工作。我该怎么做?

I'm using ValueInjecter for object mapping and I'm trying to inject from an ExpandoObject. I found an example of injecting from a dynamic.

   public class Ac
    {
        public string Aa { get; set; }
    }

    [Test]
    public void Aa()
    {
        var o = new { Aa = "aa" };
        dynamic d = o;
        var a = new Ac{ Aa = "bb" };
        a.InjectFrom((object)d);
        Assert.AreEqual(o.Aa, a.Aa);
    }

But I have not been successful in getting it to work with an ExpandoObject. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

焚却相思 2024-12-03 23:00:53
using System;
using System.Collections.Generic;
using System.Dynamic;
using Omu.ValueInjecter;

namespace ConsoleApplication7
{
    public class FromExpando : KnownSourceValueInjection<ExpandoObject>
    {
        protected override void Inject(ExpandoObject source, object target)
        {
            var d = source as IDictionary<string, object>;
            if (d == null) return;
            var tprops = target.GetProps();

            foreach (var o in d)
            {
                var tp = tprops.GetByName(o.Key);
                if (tp == null) continue;
                tp.SetValue(target, o.Value);
            }
        }
    }

    public class Foo
    {
        public string Name { get; set; }
        public int Ace { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            dynamic x = new ExpandoObject();
            x.Ace = 1231;
            x.Name = "hi";
            var f = new Foo();
            //f.InjectFrom<FromExpando>((object) x); // edit:compilation error
            new FromExpando().Map((object)x,f);
            Console.WriteLine(f.Ace);
            Console.WriteLine(f.Name);
        }
    }
} 
using System;
using System.Collections.Generic;
using System.Dynamic;
using Omu.ValueInjecter;

namespace ConsoleApplication7
{
    public class FromExpando : KnownSourceValueInjection<ExpandoObject>
    {
        protected override void Inject(ExpandoObject source, object target)
        {
            var d = source as IDictionary<string, object>;
            if (d == null) return;
            var tprops = target.GetProps();

            foreach (var o in d)
            {
                var tp = tprops.GetByName(o.Key);
                if (tp == null) continue;
                tp.SetValue(target, o.Value);
            }
        }
    }

    public class Foo
    {
        public string Name { get; set; }
        public int Ace { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            dynamic x = new ExpandoObject();
            x.Ace = 1231;
            x.Name = "hi";
            var f = new Foo();
            //f.InjectFrom<FromExpando>((object) x); // edit:compilation error
            new FromExpando().Map((object)x,f);
            Console.WriteLine(f.Ace);
            Console.WriteLine(f.Name);
        }
    }
} 
半城柳色半声笛 2024-12-03 23:00:53

我使用了与 Omu 发布的读取来自 XML 的 ExpandoObject 相同的方法。由于所有属性都以“字符串”形式出现,因此我使用“Convert.ChangeType”方法稍微调整了@Omu的答案:

public class FromExpando : KnownSourceValueInjection<ExpandoObject>
{
    protected override void Inject(ExpandoObject source, object target)
    {
        var d = source as IDictionary<string, object>;
        if (d == null) return;
        var tprops = target.GetProps();

        foreach (var o in d)
        {
            var tp = tprops.GetByName(o.Key);
            if (tp == null) continue;

            var newValue = Convert.ChangeType(o.Value, tp.PropertyType); 

            tp.SetValue(target, newValue);
        }
    }
}

I have used the same approach as Omu posted reading from a ExpandoObject that comes from a XML. As all the properties comes in as ´string´, so I have tweaked @Omu's answer a little using the ´Convert.ChangeType´ method:

public class FromExpando : KnownSourceValueInjection<ExpandoObject>
{
    protected override void Inject(ExpandoObject source, object target)
    {
        var d = source as IDictionary<string, object>;
        if (d == null) return;
        var tprops = target.GetProps();

        foreach (var o in d)
        {
            var tp = tprops.GetByName(o.Key);
            if (tp == null) continue;

            var newValue = Convert.ChangeType(o.Value, tp.PropertyType); 

            tp.SetValue(target, newValue);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文