English 中文(简体)
反差类
原标题:Instantiate typed IEnumerable of compile-known type by reflection

Using only .net 3.5 i have sample class:

public class Table1
{
   public IEnumerable<Table2> Items { get; set; }
}

其某种类型的办公室管理,即需要用一种计算法进行收集。

I trying find way to assign instance to property Items, for example by List<Table2>

When i create instance by Activator, it returns object, which i cant cast to needed type

var t = typeof(List<>);
var gt = t.MakeGenericType(typeof(Table2));
object instance = Activator.CreateInstance(gt);

var table1 = new Table1();
table1.Items = instance; //canot use cast here

把物体变量划分为可计算数字的千兆克

它如何在大多数OR-地图绘制中发挥作用?

Can i use Reflection.Emit to generate concrete type?
Can i use Castle/Linfu?

EDIT:

I canot use any direct cast, because it requires reference Table2 which is i cannot harcode

内容:

在经过一段时间后,我自己找到了解决办法。 有必要进行思考,例如:

var table1 = new Table1();
var table1Type = typeof(Table1);
var prop = table1Type.GetProperty("Items");
prop.SetValue(table1, instance, null);
问题回答

我认为,你只是找不到一个简单的东西:

而不是:

table1.Items = instance;

作出以下决定:

table1.Items = instance as IEnumerable<Table2>;

如果你知道你想要一份名单,那么你为什么只能使用<条码>新清单和提纲;Table2>? 通常,只有<条码>启动人。 当这种类型在操作时间之前就知道时,往往使用严格配置值。

但是,你们都需要做的是:instance > ,要么是IE amountable<Table2> or List<Table2>,要么是:

table1.Items = (IEnumerable<Table2>) instance;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签