将动态 C# 对象转换为数组

发布于 2024-12-10 19:07:23 字数 158 浏览 0 评论 0原文

如果动态对象是 Foo 或 Foo[] 类型的单个对象,是否有办法将其转换为数组?

例如,

如果动态是 Foo,则转换为数组中包含 1 个对象的 Foo[]

,如果动态是包含 n 个对象的数组 Foo[],则转换为数组中包含 n 个对象的 Foo[]。

Is there a way to convert a dynamic object to an array if it is either a single object of type Foo or Foo[] ?

For example,

if dynamic is Foo then convert to Foo[] with 1 object in array

if dynamic is an array Foo[] with n number of objects then convert to Foo[] with n number of objects in array.

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

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

发布评论

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

评论(3

岁月打碎记忆 2024-12-17 19:07:23

我觉得自己有点傻……这真的是你想要的吗?

class Test
{
}

dynamic dyn = new Test();

Test[] tests = null;

if (dyn is Test)
{
    tests = new Test[] { (Test)dyn };
}
else if (dyn is Test[])
{
    tests = (Test[])dyn;
}

I feel a little stupid... Is really this you want?

class Test
{
}

dynamic dyn = new Test();

Test[] tests = null;

if (dyn is Test)
{
    tests = new Test[] { (Test)dyn };
}
else if (dyn is Test[])
{
    tests = (Test[])dyn;
}
无人问我粥可暖 2024-12-17 19:07:23
public Foo[] Create(dynamic fooOrFoos)
{
    return fooOrFoos.GetType().IsArray ? fooOrFoos as Foo[] : new Foo[] { fooOrFoos };
}

也许我让这太简单了......您是否暗示您对动态值可能是什么类型一无所知?

public Foo[] Create(dynamic fooOrFoos)
{
    return fooOrFoos.GetType().IsArray ? fooOrFoos as Foo[] : new Foo[] { fooOrFoos };
}

Perhaps I'm making this too easy... are you implying that you know nothing about what type the dynamic value might be?

り繁华旳梦境 2024-12-17 19:07:23

通过使用动态变量,变量是动态的,而不是变量所引用的对象。也就是说,当您想要访问对象的成员或想要转换它时,没有静态类型检查。你需要一些其他的魔法才能做到这一点。

您可以将 Foo 包装在 DynamicObject 中,您可以在其中指定如何进行转换。

public class DynamicWrapper<T> : DynamicObject
{
    public T Instance { get; private set; }
    public DynamicWrapper(T instance)
    {
        this.Instance = instance;
    }

    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        if (binder.ReturnType == typeof(T))
        {
            result = Instance;
            return true;
        }
        if (binder.ReturnType == typeof(T[]) && binder.Explicit)
        {
            result = new[] { Instance };
            return true;
        }
        return base.TryConvert(binder, out result);
    }

    public override string ToString()
    {
        return Convert.ToString(Instance);
    }
}

然后你可以这样做:

dynamic dobj = new DynamicWrapper<Foo>(someFoo);
Foo foo1 = dobj;
Foo foo2 = (Foo)dobj;
Foo[] arr1 = (Foo[])dobj;
// someFoo == foo1 == foo2 == arr1[0]

dynamic darr = new DynamicWrapper<Foo[]>(arr1);
Foo[] arr2 = darr;
Foo[] arr3 = (Foo[])darr;
// arr1 == arr2 == arr3
// someFoo == arr2[0] == arr3[0]

By using a dynamic variable, the variable is dynamic, not the object the variable is referring to. That is, there is no static type checking when you want to access members of the object or want to convert it. You need some other magic to be able to do that.

You could wrap your Foo in a DynamicObject where you can specify how the conversion takes place.

public class DynamicWrapper<T> : DynamicObject
{
    public T Instance { get; private set; }
    public DynamicWrapper(T instance)
    {
        this.Instance = instance;
    }

    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        if (binder.ReturnType == typeof(T))
        {
            result = Instance;
            return true;
        }
        if (binder.ReturnType == typeof(T[]) && binder.Explicit)
        {
            result = new[] { Instance };
            return true;
        }
        return base.TryConvert(binder, out result);
    }

    public override string ToString()
    {
        return Convert.ToString(Instance);
    }
}

Then you could do this:

dynamic dobj = new DynamicWrapper<Foo>(someFoo);
Foo foo1 = dobj;
Foo foo2 = (Foo)dobj;
Foo[] arr1 = (Foo[])dobj;
// someFoo == foo1 == foo2 == arr1[0]

dynamic darr = new DynamicWrapper<Foo[]>(arr1);
Foo[] arr2 = darr;
Foo[] arr3 = (Foo[])darr;
// arr1 == arr2 == arr3
// someFoo == arr2[0] == arr3[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文