Tag: reflection

使用Reflection创建HttpPostedFile的实例

有没有办法用Reflection创建HttpPostedFile的实例。 我试过了: var obj = (HttpPostedFile)typeof(HttpPostedFile).GetConstructor( BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null).Invoke(null); var obj2 = Activator.CreateInstance(typeof(HttpPostedFile) , BindingFlags.NonPublic | BindingFlags.Instance , null , new object[] { } , System.Globalization.CultureInfo.CurrentCulture); 但他们俩都行不通。 更新:显然内部构造函数如下所示: internal HttpPostedFile(string filename, string contentType, Stream stream) 我试过这个但没有成功: Stream s = new MemoryStream(b.blob); //var httpPostedFile = new HttpPostedFile(); Type[] parameters = { typeof(string), typeof(string), […]

指示generics返回动态类型的对象

这个问题是我原来问题的后续问题。 假设我有以下generics类(简化!^ _ ^): class CasterClass where T : class { public CasterClass() { /* none */ } public T Cast(object obj) { return (obj as T); } } 哪个能够将对象转换为指定的类型。 不幸的是,在编译时,我不知道我将要使用哪种类型,所以我必须通过reflection来实例化这个类,如下所示: Type t = typeof(castedObject); // creating the reflected Caster object object CasterObj = Activator.CreateInstance( typeof(CasterClass).MakeGenericType(t) ); // creating a reflection of the CasterClass’ Cast […]

在运行时提取类型

我有一个类型名称作为输入传递。 “MyApp.Modules.Common.contact”。 使用Activator.CreateInstance如何在我正在使用的方法中构造此类型。 如果我这样做的话 Type typ = Type.GetType(“MyApp.Modules.Common.contact”) 典型值始终为空。 我该如何解决。 请帮忙。

在C#中获取属性名称作为字符串

是否可以编写一个返回对象属性的字符串值的函数? 如果我有一个名为apple的对象,它有一个名为peel的方法,我希望有一个方法在调用getAttributeName(apple.peel)时返回“peel”。 我该怎么做?

通过reflectionC#获取嵌套属性值

我有以下代码。 类别: public class AlloyDock { public int Left { get; set; } public int Right { get; set; } } public class Charger { public int Left { get; set; } public int Right { get; set; } } public class VehicleControlTest { public Charger Charger1 { get; set; } } public class BasicControlTest […]

C#:有没有办法使用reflection调整未知类型的数组?

我的用户传给我一些类型的数组,比如int []或string []。 我可以通过GetElementType轻松查询元素的类型,我可以通过GetRank,GetLength等找到数组传递给我的时间。 数组在params列表中传递,因此可视化代码如下: public void Resizer(params object[] objs) { foreach (object o in objs) Array.Resize(ref o, 3); } 我想要做的是与可用的Get方法相反并且确实有效:我想调整传递给我的数组的大小,将长度设置为其他长度(在这个愚蠢的例子中就像3)。 我这样做是因为在我的设置中,数组将包含从一组云计算服务器接收的数据,我们无法知道有多少人会提前响应,因此无法预先分配数组以获得正确的长度。 理想情况下,实际上,我的用户传入一个长度为0的数组,然后传回一个长度为n的数组,表示我从查询服务器获得了n个回复。 我无法使用Array.Resize(ref T,int)执行此操作,因为我在编译时不知道T. 有没有办法解决这个问题?

MethodInfo.Invoke中的Uncatcheableexception

我有这个代码调用一个MethodInfo: try { registrator.Method.Invoke(instance, parameters); } catch{ registrator.FailureType = RegistratorFailureType.ExceptionInRegistrator; //registrator.Exception = e; } Registrator只是一个MethodInfo包装器,Method属性是MethodInfo对象本身。 参数是和object []和实例是Method的声明类型的正确实例(使用Activator.Create创建)。 方法看起来像这样(我正在测试exception捕获): class Test : Plugin, ITest { public void Register(IWindow window) { throw new Exception(“Hooah”); } } 问题是:exception永远不会被捕获,并且会弹出Visual Studio未捕获的exception气泡。 这是在使用.NET 4.0的VS 2010中

序列化/反序列化和非默认构造函数

考虑这个课程: [Persistable] public sealed class FileMoveTask : TaskBase { [PersistMember] public string SourceFilePath { get; private set;} [PersistMember] public string DestFilePath { get; private set;} public FileMoveTask(string srcpath, string dstpath) { this.SourceFilePath = srcpath; this.DestFilePath = dstpath; //possibly other IMPORTANT initializations } //code } 我可以通过使用属性PersistMember序列化所有成员来持久保存此类的对象。 但是在反序列化过程中我遇到了一些问题(设计问题)。 特别是,问题在于构造函数中可能存在的“可能的其他重要初始化” ,并且程序员可能决定不会使少数成员可持久化(即不PersistMember添加PersistMember )可能因为这没有意义。 在这种情况下,我如何将对象反序列化为完全相同的状态? 我想,这个问题归结为:我如何调用非默认构造函数,将相同的参数传递给它,之前传递过? 有没有办法做到这一点? 我们可以制定一些可以由编译器强制执行的规则(一种元编程)吗? 构造函数属性可以在这帮助吗?

当参数是通用的时,Type.GetMethod的Type数组中应该包含哪些类型?

如果我想通过reflection调用generics方法,我可以很容易地使用这种技术,除非: 该方法只能通过其参数区分。 该方法有一个参数,其类型是方法的类型参数之一。 调用Type.GetMethod(string, Type[])时,如何在Type[]数组中指定generics参数? 例: public class Example { //This is the one I want to call. public void DoSomething(T t) { … } public void DoSomething(Foo foo) { … } public void CallDoSomething(Type type, object value) { MethodInfo method = typeof(Example) .GetMethod(“DoSomething”, new Type[] {/* what do i put here? */ }); MethodInfo […]

通过C#reflection获取类的Enum

我有一个Enum喜欢 namespace EnumTest { public class Enumeration { public Enumeration(); public enum Days { day = sunday, night = monday } } } 如何通过反思获取类型信息数天。 Type type = assembly.GetType(Days); Type type = typeof(Days)将返回Type type = typeof(Days)的类型信息。 如果我有String s = “Days” ,使用此字符串s我需要获取Days的类型信息。 我需要类型=天