使用泛型可以实现这一点吗?时间:2019-03-17 标签:c#

发布于 2024-09-26 04:47:56 字数 358 浏览 4 评论 0 原文

我知道我可以通过创建自定义类来解决以下问题,但是以下内容可以强类型化为 List(或任何其他类型)吗?

 var x = new object[] 
        { 
            new object[] { new Image(), new TextBox(), new FileUpload() }, 
            new object[] { new Image(), new TextBox() , new FileUpload()} 
        };

上面代码中的对象类型只是举例。

一天结束了,我的大脑变得有点软了。

编辑:元组?

I know I can solve the following problem just by creating a custom class, but can the following be strongly typed as a List(or any other type)?

 var x = new object[] 
        { 
            new object[] { new Image(), new TextBox(), new FileUpload() }, 
            new object[] { new Image(), new TextBox() , new FileUpload()} 
        };

The object types in the code above are just for example.

It's the end of the day and my brain has gone abit soft.

Edit: Tuples?

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

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

发布评论

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

评论(6

夕嗳→ 2024-10-03 04:47:56

是的,通用元组可以工作:

http://sankarsan .wordpress.com/2009/11/29/tuple-in-c-4-0/

var myTuples = new List<Tuple<type1, type2, type3>>();

Yep, generic Tuples would work:

http://sankarsan.wordpress.com/2009/11/29/tuple-in-c-4-0/

var myTuples = new List<Tuple<type1, type2, type3>>();
百善笑为先 2024-10-03 04:47:56
var x = new []         
{             
    new 
    { 
        Image = new Image(), 
        TextBox = new TextBox(), 
        FileUpload = new FileUpload() 
    },
    new 
    { 
        Image = new Image(), 
        TextBox = new TextBox(), 
        FileUpload = new FileUpload()
    }         
};
var x = new []         
{             
    new 
    { 
        Image = new Image(), 
        TextBox = new TextBox(), 
        FileUpload = new FileUpload() 
    },
    new 
    { 
        Image = new Image(), 
        TextBox = new TextBox(), 
        FileUpload = new FileUpload()
    }         
};
行至春深 2024-10-03 04:47:56

您没有指定,但看起来您正在使用 System.Web 中的类型?如果是这样,那么可以使用泛型来创建更强类型的集合。例如,

List<List<WebControl>> list = new List<List<WebControl>>();
list.Add(new List<WebControl>(new WebControl()[] new Image(), new TextBox(), new FileUpload
() );
list.Add(new List<WebControl>(new WebControl()[] new Image(), new TextBox(), new FileUpload() );

为了获得一个非常强类型的集合,您需要生成 Tuple<> 或匿名类型解决方案。

You didn't specify but it looks like you are using the types from System.Web? If so then yes generics can be used to create a more strongly typed collection. For example

List<List<WebControl>> list = new List<List<WebControl>>();
list.Add(new List<WebControl>(new WebControl()[] new Image(), new TextBox(), new FileUpload
() );
list.Add(new List<WebControl>(new WebControl()[] new Image(), new TextBox(), new FileUpload() );

In order to get a very strongly typed collection though you'll need to result to Tuple<> or an anonymous type solution.

流殇 2024-10-03 04:47:56

你可以像你说的那样使用元组。或者匿名类型:

var x = new[] 
    { 
        new { Image = new Image(), TextBox = new TextBox(), FileUpload = new FileUpload() }, 
        new { Image = new Image(), TextBox = new TextBox(), FileUpload = new FileUpload() }
    };

You can use Tuple as you say. Or anonymous type:

var x = new[] 
    { 
        new { Image = new Image(), TextBox = new TextBox(), FileUpload = new FileUpload() }, 
        new { Image = new Image(), TextBox = new TextBox(), FileUpload = new FileUpload() }
    };
不必了 2024-10-03 04:47:56

匿名对象强类型的。
唯一的问题是您无法知道类型名称(不是直接知道)。

拿这个例子(对不起,如果太长了):

        static void T2( )
    {
        var x = new
        {
            a = new { a1 = new Type1( "x.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "x.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        var y = new
        {
            a = new { a1 = new Type1( "y.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "y.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        var z = new
        {
            a = new { a1 = new Type1( "y.1" ), a2 = new Type3( '1' ) },
            b = new { b1 = new Type3( 'z' ), b2 = new Type2( 2 ) }
        };

        Console.WriteLine( new string( '-', 40 ) );
        Console.WriteLine( "Anonymous object \"x\" is named {0}.", x.GetType( ) );
        Console.WriteLine( "Anonymous object \"y\" is named {0}.", y.GetType( ) );
        Console.WriteLine( "Anonymous object \"z\" is named {0}.", z.GetType( ) );

        Console.WriteLine( new string( '-', 40 ) );
        Console.Write( "Anonymous object \"x\" == \"y\"? " );
        Console.WriteLine( x.Equals( y ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        var x2 = new
        {
            a = new { a1 = new Type1( "x.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "x.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        Console.Write( "Anonymous object \"x\" == \"x2\"? " );
        Console.WriteLine( x.Equals( x2 ) ? "Yes" : "No" );

        // Uncomment it to give:
        //Error 1   Cannot implicitly convert type 'AnonymousType#1' to 'AnonymousType#2'
#if GiveMeAnError
        z = new
        {
            a = new { a1 = new Type1( "z.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "z.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        Console.WriteLine( "Anonymous object \"z\" now is named {0}.", z.GetType( ) );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );
#endif
        Console.ReadKey( );
    }

它输出:

/*----------------------------------------
Anonymous object "x" is named <>f__AnonymousType2`2[<>f__AnonymousType0`3    [anon_obj.Type1,anon_obj.Type2,anon_obj.Type3],<>f__AnonymousType1`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3]].
Anonymous object "y" is named <>f__AnonymousType2`2[<>f__AnonymousType0`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3],<>f__AnonymousType1`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3]].
Anonymous object "z" is named <>f__AnonymousType2`2[<>f__AnonymousType3`2[anon_obj.Type1,anon_obj.Type3],<>f__AnonymousType4`2[anon_obj.Type3,anon_obj.Type2]].
----------------------------------------
Anonymous object "x" == "y"? No
Anonymous object "x" == "z"? No
Anonymous object "x" == "x2"? Yes*/

每个匿名对象组合都有自己的名称并定义唯一的类型。
使用相同类型和类型名称声明的对象将具有相同的类型,如“x == x2”。

不过,最初的示例很棘手,因为它定义了内部包含“object[]”数组的“object[]”数组。
这样,

            var x = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        var y = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("y.2"), new Type2(2) , new Type3('2')}  
        };

        var z = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type3('1') },  
            new object[] { new Type3('z'), new Type2(2)}  
        };

所有的类型(object[])都会相同,并且比较将始终通过比较指针来完成,希望有所不同。

        static void T1( )
    {
        var x = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        var y = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("y.2"), new Type2(2) , new Type3('2')}  
        };

        var z = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type3('1') },  
            new object[] { new Type3('z'), new Type2(2)}  
        };

        Console.WriteLine( new string( '-', 40 ) );
        Console.WriteLine( "Anonymous object \"x\" is named {0}.", x.GetType( ) );
        Console.WriteLine( "Anonymous object \"y\" is named {0}.", y.GetType( ) );
        Console.WriteLine( "Anonymous object \"z\" is named {0}.", z.GetType( ) );


        Console.WriteLine( new string( '-', 40 ) );
        Console.Write( "Anonymous object \"x\" == \"y\"? " );
        Console.WriteLine( x.Equals( y ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        var x2 = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        Console.Write( "Anonymous object \"x\" == \"x2\"? " );
        Console.WriteLine( x.Equals( x2 ) ? "Yes" : "No" );

        z = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        Console.WriteLine( "Anonymous object \"z\" now is named {0}.", z.GetType( ) );


        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\" (memberwise)? " );
        Console.WriteLine(
            x[ 0 ].Equals( z[ 0 ] )
            && x[ 1 ].Equals( z[ 1 ] )
            ? "Yes" : "No" );

        Console.ReadKey( );
    }

将输出:

/*----------------------------------------
Anonymous object "x" is named System.Object[].
Anonymous object "y" is named System.Object[].
Anonymous object "z" is named System.Object[].
----------------------------------------
Anonymous object "x" == "y"? No
Anonymous object "x" == "z"? No
Anonymous object "x" == "x2"? No
Anonymous object "z" now is named System.Object[].
Anonymous object "x" == "z"? No
Anonymous object "x" == "z" (memberwise)? No
----------------------------------------*/

看到问题了吗?

Anonymous objects are strongly typed.
The only concern is that you can't know the type name (not directly).

Take this example (sorry if it is too long):

        static void T2( )
    {
        var x = new
        {
            a = new { a1 = new Type1( "x.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "x.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        var y = new
        {
            a = new { a1 = new Type1( "y.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "y.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        var z = new
        {
            a = new { a1 = new Type1( "y.1" ), a2 = new Type3( '1' ) },
            b = new { b1 = new Type3( 'z' ), b2 = new Type2( 2 ) }
        };

        Console.WriteLine( new string( '-', 40 ) );
        Console.WriteLine( "Anonymous object \"x\" is named {0}.", x.GetType( ) );
        Console.WriteLine( "Anonymous object \"y\" is named {0}.", y.GetType( ) );
        Console.WriteLine( "Anonymous object \"z\" is named {0}.", z.GetType( ) );

        Console.WriteLine( new string( '-', 40 ) );
        Console.Write( "Anonymous object \"x\" == \"y\"? " );
        Console.WriteLine( x.Equals( y ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        var x2 = new
        {
            a = new { a1 = new Type1( "x.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "x.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        Console.Write( "Anonymous object \"x\" == \"x2\"? " );
        Console.WriteLine( x.Equals( x2 ) ? "Yes" : "No" );

        // Uncomment it to give:
        //Error 1   Cannot implicitly convert type 'AnonymousType#1' to 'AnonymousType#2'
#if GiveMeAnError
        z = new
        {
            a = new { a1 = new Type1( "z.1" ), a2 = new Type2( 1 ), a3 = new Type3( '1' ) },
            b = new { b1 = new Type1( "z.2" ), b2 = new Type2( 2 ), b3 = new Type3( '2' ) }
        };

        Console.WriteLine( "Anonymous object \"z\" now is named {0}.", z.GetType( ) );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );
#endif
        Console.ReadKey( );
    }

It outputs:

/*----------------------------------------
Anonymous object "x" is named <>f__AnonymousType2`2[<>f__AnonymousType0`3    [anon_obj.Type1,anon_obj.Type2,anon_obj.Type3],<>f__AnonymousType1`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3]].
Anonymous object "y" is named <>f__AnonymousType2`2[<>f__AnonymousType0`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3],<>f__AnonymousType1`3[anon_obj.Type1,anon_obj.Type2,anon_obj.Type3]].
Anonymous object "z" is named <>f__AnonymousType2`2[<>f__AnonymousType3`2[anon_obj.Type1,anon_obj.Type3],<>f__AnonymousType4`2[anon_obj.Type3,anon_obj.Type2]].
----------------------------------------
Anonymous object "x" == "y"? No
Anonymous object "x" == "z"? No
Anonymous object "x" == "x2"? Yes*/

Each anonymous object composition has its own name and defines an unique type.
Objects declared with the same types and type names go to the same type, as in "x == x2".

The original example, though, is tricky, as it defines "object[]" arrays with "object[]" arrays inside.
This way

            var x = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        var y = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("y.2"), new Type2(2) , new Type3('2')}  
        };

        var z = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type3('1') },  
            new object[] { new Type3('z'), new Type2(2)}  
        };

Will all be the same type (object[]), and the comparison will be allways done by comparing pointers, that will, hopefuly, differ.

        static void T1( )
    {
        var x = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        var y = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("y.2"), new Type2(2) , new Type3('2')}  
        };

        var z = new object[ ]  
        {  
            new object[] { new Type1("y.1"), new Type3('1') },  
            new object[] { new Type3('z'), new Type2(2)}  
        };

        Console.WriteLine( new string( '-', 40 ) );
        Console.WriteLine( "Anonymous object \"x\" is named {0}.", x.GetType( ) );
        Console.WriteLine( "Anonymous object \"y\" is named {0}.", y.GetType( ) );
        Console.WriteLine( "Anonymous object \"z\" is named {0}.", z.GetType( ) );


        Console.WriteLine( new string( '-', 40 ) );
        Console.Write( "Anonymous object \"x\" == \"y\"? " );
        Console.WriteLine( x.Equals( y ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        var x2 = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        Console.Write( "Anonymous object \"x\" == \"x2\"? " );
        Console.WriteLine( x.Equals( x2 ) ? "Yes" : "No" );

        z = new object[ ]  
        {  
            new object[] { new Type1("x.1"), new Type2(1), new Type3('1') },  
            new object[] { new Type1("x.2"), new Type2(2) , new Type3('2')}  
        };

        Console.WriteLine( "Anonymous object \"z\" now is named {0}.", z.GetType( ) );


        Console.Write( "Anonymous object \"x\" == \"z\"? " );
        Console.WriteLine( x.Equals( z ) ? "Yes" : "No" );

        Console.Write( "Anonymous object \"x\" == \"z\" (memberwise)? " );
        Console.WriteLine(
            x[ 0 ].Equals( z[ 0 ] )
            && x[ 1 ].Equals( z[ 1 ] )
            ? "Yes" : "No" );

        Console.ReadKey( );
    }

Will output:

/*----------------------------------------
Anonymous object "x" is named System.Object[].
Anonymous object "y" is named System.Object[].
Anonymous object "z" is named System.Object[].
----------------------------------------
Anonymous object "x" == "y"? No
Anonymous object "x" == "z"? No
Anonymous object "x" == "x2"? No
Anonymous object "z" now is named System.Object[].
Anonymous object "x" == "z"? No
Anonymous object "x" == "z" (memberwise)? No
----------------------------------------*/

See the gotcha?

無處可尋 2024-10-03 04:47:56

如果您使用的是框架 4,则 Tuple[]List> 即可解决问题。

否则,您可以使用匿名类型,尽管除此之外可能会很尴尬(因为您无法在有用的情况下声明该类型)。

最后,自行构建并不是最棘手的结构。

A Tuple<Image, TextBox, FileUpload>[] or perhaps List<Tuple<Image, TextBox, FileUpload>> will do the trick if you are using framework 4.

Otherwise you could use an anonymous type, though it can be awkward beyond that (since you can't declare the type in cases where that is useful).

Finally, it's not the trickiest structure to just roll your own.

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