小编Ein*_*jar的帖子

无法将List <int [*]>强制转换为使用反射实例化的List <int []>

我通过反射实例化一个List<T>单维Int32数组.当我使用以下方法实例化列表时:

Type typeInt = typeof(System.Int32);
Type typeIntArray = typeInt.MakeArrayType(1);
Type typeListGeneric = typeof(System.Collections.Generic.List<>);
Type typeList = typeListGeneric.MakeGenericType(new Type[] { typeIntArray, });

object instance = typeList.GetConstructor(Type.EmptyTypes).Invoke(null);
Run Code Online (Sandbox Code Playgroud)

我在列表上看到了这种奇怪的行为:

将列表实例视为对象

如果我通过反射与它接口,它似乎表现正常,但是如果我尝试将它转换为它的实际类型:

List<int[]> list = (List<int[]>)instance;
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

无法将类型为'System.Collections.Generic.List`1 [System.Int32 [*]]'的对象强制转换为'System.Collections.Generic.List`1 [System.Int32 []]'.

可能导致此问题或如何解决问题的任何想法?我在.net 4.0上的visual studio 2010 express工作.

c# generics reflection list

11
推荐指数
1
解决办法
222
查看次数

ADO.net SQL Server 2012 - 使用复合键进行查询,并且MissingSchemaAction.AddWithKey错误地添加了约束

此查询在ADO.net上运行时MissingSchemaAction.AddWithKey抛出异常:

无法启用约束.一行或多行包含违反非null,唯一或外键约束的值.

查询:

SELECT map.GroupId, b.PersonId 
FROM [GroupPersonMap] as map
INNER JOIN [Person] AS b ON b.PersonId = map.PersonId
GROUP BY map.GroupId, b.PersonId 
Run Code Online (Sandbox Code Playgroud)

检查本地人显示PersonId已添加了一个独特的约束.不仅如此,在SQL Server Manager中运行相同的查询还会返回结果集,而不会出现任何警告或错误.这个确切的代码用于在SQL Server 2005上工作.使用SQL Server 2005,在ADO.net上运行此查询时,查询会正确创建复合约束.这是升级问题吗?

作为旁注,我知道该设置EnforceConstraints = false提供了一种解决方法.但理想情况下,我想从根本上解决这个问题.

设置重现:

CREATE TABLE [GroupPersonMap]
(
[GroupId] [int] NOT NULL,
[PersonId] [int] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [GroupPersonMap] ADD CONSTRAINT [PK_GroupPersonMAP] PRIMARY KEY CLUSTERED  ([GroupId], [PersonId])

CREATE TABLE [Person]
(
[PersonId] [int] NOT NULL IDENTITY(1, 1),
[Val] INT
) …
Run Code Online (Sandbox Code Playgroud)

t-sql ado.net sql-server-2012

5
推荐指数
1
解决办法
434
查看次数

标签 统计

ado.net ×1

c# ×1

generics ×1

list ×1

reflection ×1

sql-server-2012 ×1

t-sql ×1