组合两种匿名类型

发布于 2024-12-03 20:44:52 字数 1078 浏览 0 评论 0原文

我之前问过相关问题并得到了很好的答复 我如何定义匿名类型?< /a>

但现在我想知道是否可以组合两种匿名类型

基本上我在缓存中存储一​​些每周数据,就像我在相关问题中描述的那样

所以如果我将匿名类型存储为object 它包含本周的一些数据 我想每天将今天的数据附加到该对象。

因此,我从一侧将匿名类型中的数据作为对象,从另一侧我有完全相同的结构化匿名类型,并且我想要附加/将新的匿名类型中的数据添加到旧的匿名类型作为对象并将其放回缓存。

如果您查看我的相关问题,我的匿名类型看起来像这样:

var jsonData = new { total = 0, page = 0, records = 0,
                     rows = new[] { new { dummy = "" } };

那么问题是我如何在我的情况下组合匿名类型?

例如,如果它是两个集合,我会这样做

List<Data> weeklyFromCahce = GetFromCache()
List<Data> todaysToCahce = GetFromDataBase();

,然后循环遍历 todaysToCahce 并将每个项目添加到 weeklyFromCahce 并更新 weeklyFromCahce代码>返回缓存。

所以我想做类似的过程,但在我的例子中使用匿名类型。组合结果应该是匿名类型,其中包含每周数据+当天数据,并且我使用新的匿名类型更新缓存,该新匿名类型将包含两个匿名实例的组合结果。

I asked the related question before and get gretat ansfer on that How could i define anonymous type?

But now i am wondering is it possible to combine two anonymous types

Basically i am storing some weekly data in cache like i described in related queston

So in case i have anonymous type stored as object it is contain some data for current week
and i want to append today's data to that object everyday.

So i have data in anonymous type as object from one side and from another i have completely the same structured anonymous type and i want do append/add data from my new anonymous type to the old one which is anonymous type as object and put it back to cache.

If you would look my related question my anonymous type looking like that:

var jsonData = new { total = 0, page = 0, records = 0,
                     rows = new[] { new { dummy = "" } };

So the question is how could i combine anonymous types in my case?

For example if it would be two collections i would go like that

List<Data> weeklyFromCahce = GetFromCache()
List<Data> todaysToCahce = GetFromDataBase();

and than i would loop though todaysToCahce and add each item to weeklyFromCahce and that put updated weeklyFromCahce back to cache.

So i want to do similar process but with anonymous types in my case. The combined result should be the anonymous type which would contain weekly data + current day data and that i update cache with the new anonymous type which would contain combined result of two anonymous instances.

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

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

发布评论

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

评论(1

季末如歌 2024-12-10 20:44:52
var x = new { member1 = 123 };
var y = new { member2 = "qwerty" };
var z = new { part1 = x, part2 = y }; // combined

在 TS 评论后更新

var x = new { member1 = 123 };
var y = new { member2 = "qwerty" };
var z = new { member1 = x.member1, member2 = y.member2 }; // combined

更新 2

class DynamicPair<TX, TY> : DynamicObject
{
    private readonly TX _x;
    private readonly TY _y;

    public DynamicPair(TX x, TY y)
    {
        _x = x;
        _y = y;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        try
        {
            var property = typeof (TX).GetProperty(binder.Name);
            if (property != null)
            {
                result = property.GetValue(_x, null);
                return true;
            }

            property = typeof (TY).GetProperty(binder.Name);
            if (property != null)
            {
                result = property.GetValue(_y, null);
                return true;
            }
        }
        catch
        {
        }

        result = null;
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        // too lazy
        return true;
    }
}


class Program
{
    private static DynamicPair<TX, TY> Combine<TX, TY>(TX x, TY y)
    {
        return new DynamicPair<TX, TY>(x, y);
    }

    static void Main(string[] args)
    {
        var x = new { a = 123 };
        var y = new { b = 222 };
        dynamic z = Combine(x, y);
        Console.WriteLine("{0}, {1}", z.a, z.b);
    }
}

不确定它是否适合您的情况,但仍然有效。

var x = new { member1 = 123 };
var y = new { member2 = "qwerty" };
var z = new { part1 = x, part2 = y }; // combined

?

Updated after TS comment

var x = new { member1 = 123 };
var y = new { member2 = "qwerty" };
var z = new { member1 = x.member1, member2 = y.member2 }; // combined

Update 2

class DynamicPair<TX, TY> : DynamicObject
{
    private readonly TX _x;
    private readonly TY _y;

    public DynamicPair(TX x, TY y)
    {
        _x = x;
        _y = y;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        try
        {
            var property = typeof (TX).GetProperty(binder.Name);
            if (property != null)
            {
                result = property.GetValue(_x, null);
                return true;
            }

            property = typeof (TY).GetProperty(binder.Name);
            if (property != null)
            {
                result = property.GetValue(_y, null);
                return true;
            }
        }
        catch
        {
        }

        result = null;
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        // too lazy
        return true;
    }
}


class Program
{
    private static DynamicPair<TX, TY> Combine<TX, TY>(TX x, TY y)
    {
        return new DynamicPair<TX, TY>(x, y);
    }

    static void Main(string[] args)
    {
        var x = new { a = 123 };
        var y = new { b = 222 };
        dynamic z = Combine(x, y);
        Console.WriteLine("{0}, {1}", z.a, z.b);
    }
}

Not exactly sure if it's acceptable in your case, but still it works.

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