Tag: reflection

如何获取托管类的原始内存指针?

如何在C#中找到指向托管类的原始指针,希望它是内存中的原始大小? 显然,CLR不允许这样做 – 更确切地说,严格禁止,因为管理类的非托管表示永远不应该出于稳定性和安全性原因而被处理 – 所以我正在寻找一个黑客。 我不是在寻找序列化 – 我确实需要托管类的转储,因为它在原始内存中表示。 更准确地说,我在下面的例子中寻找类似函数getObjectPtr东西: IntPtr getObjectPtr(Object managedClass) {…} void main() { var test=new TestClass(); IntPtr* ptr_to_test=getObjectPtr(test); Console.WriteLine(ptr_to_test.ToString()); } 提前致谢! 编辑:我终于找到了自己的解决方案,并且当回来发布它作为答案时,对于已经发布的答案很快就完全感到惊讶…感谢大家! 这非常快,完全出乎意料。 最接近我的解决方案是@thehennyy的一个,但是我没有发布它,因为@Chino提出了更好的一个(抱歉我一开始就把它弄错了,我只是忘了再次取消引用指针)。 它不需要代码不安全,而且要求GC更容忍: class Program { // Here is the function in case anyone needs it. // Note, though, it does not preserve the handle while you work with […]

我能否以某种方式评估表达式以确定并可能设置null属性?

我有一个服务,它接受一个对象,并根据其中的属性将执行不同的操作; 使用此任何这些属性都可以为null,这意味着不执行此操作。 我正在尝试创建一个非常简单的API,以便在某些属性可以是多个级别的情况下执行此操作,这是当前实现的示例 service.PerformActions(DataFactory.GetNewData ( data => data.SomeParent = DataFactory.GetNewData(), data => data.SomeParent.SomeProperty = “someValue” )); 这是一个略微简化的版本,在实际情况下,我有时必须以这种方式设置多个父属性,以便在底部设置一个字符串属性。 我想要做的是调整GetNewData方法中的代码来处理根据需要实例化这些属性,以便代码看起来像这样: service.PerformActions(DataFactory.GetNewData ( data => data.SomeParent.SomeProperty = “someValue” )); 这是我目前的GetNewData代码: public static T GetNewData(params Action[] actions) { var data = Activator.CreateInstance(); foreach (var action in actions) { try { action(data); } catch (NullReferenceException) { throw new Exception(“The property […]

在运行时分配类型

我有一个类型为T的变量x和一个字符串中的值。 例如我有: bool x, value = “True” int x, value = “1” 是否有通用的方法将值赋值/解析/反序列化为x? 请注意,T可能被引用或原始类型!

反映MemberInfo到Func

我正在寻找一种方法将MemberInfo实例转换为“Func”类型(稍后通过lambda表达式使用它)。 让我们说我有一个类型的成员函数 public bool func(int); 使用reflection,我以某种方式得到MemberInfo “mi”的实例,现在我想将它转换为Func; 类型。 就像是: Func<int, bool f = myType.GetMember(mi.Name); 有办法吗? PS。 Marc Grawell的回答解决了我的问题,无需进一步评论

如何通过reflection找到重载方法

这是我之前提出的另一个问题的问题 。 我有一个重载的方法: public void Add(SomeType some) { } public void Add(AnotherType another) { } 如何通过reflection找到每种方法? 例如,如何通过reflection获得Add(SomeType some)方法? 你能帮我吗? 提前致谢。

如何通过使用lambda表达式作为参数进行reflection来调用方法?

我想做这个: MethodInfo m = myList.GetType().GetMethod(“ConvertAll”, System.Reflection.BindingFlags.InvokeMethod).MakeGenericMethod(typeof(object)); List myConvertedList = (List)m.Invoke(myList, new object[]{ (t => (object)t)}); myList是特定类型的通用列表(应用程序未知),我想将其转换为对象列表以执行某些操作。 但是这会失败,并显示以下错误:“无法将lambda表达式转换为’object’类型,因为它不是委托类型” 你能帮我找到什么问题吗? 我想做一些不可能的事吗? 有没有其他方法来实现同样的事情?

是否可以使用托管代码中的C#reflection调用非托管代码?

是否有可能使用reflection和C#.NET在.NET发布之前调用动态不同的函数(带参数)来编写C或C ++(非托管代码)? 和smole C#示例如果可能的话将不胜感激! 谢谢! Br,米兰。

如何在.NET中检测运行时类的存在?

是否有可能在.NET应用程序(C#)中有条件地检测是否在运行时定义了类? 示例实现 – 假设您要基于配置选项创建类对象?

使用CustomAttributes和GetCustomAttributes()的优势

我今天注意到我的intellisense中出现了一些新的属性,用于.NET 4.5项目的System.Type对象。 其中一个名为CustomAttributes 。 我对此很感兴趣,因为我之前已经知道GetCustomAttributes是最昂贵的reflection调用之一(当然,除了DynamicInvoke之外)。 据我所知,每次调用GetCustomAttributes导致调用属性的构造函数(从而调用内存)。 我经常使用单独缓存自定义属性来避免在处理大量类型等时出现性能瓶颈。 所以,我编写了一个测试,看看CustomAttributes是否比GetCustomAttributes更GetCustomAttributes : static void Main(string[] args) { var sw = Stopwatch.StartNew(); Debug.WriteLine(typeof(Attributed).GetType()); for (int i = 0; i a.AttributeType) .ToList(); } sw.Stop(); Debug.WriteLine(“Using .NET 4.5 CustomAttributes property: {0}”, sw.Elapsed); sw = Stopwatch.StartNew(); for (int i = 0; i a.GetType()) .ToList(); } sw.Stop(); Debug.WriteLine(“Using GetCustomAttributes method: {0}”, sw.Elapsed); } […]

函数返回一个generics类型,其值仅在运行时已知

我需要使用如下通用接口: public interface IContainer { IEnumerable<IContent> Contents { get; } } 实现此接口的对象由以下generics方法返回: IContainer GetContainer(IProperty property); 直到运行时,类型T才是未知的。 使用reflection我可以调用GetContainer方法并获得结果。 我的问题是我不知道如何枚举具有Object类型的结果(因此我无法将其IEnumerable为IEnumerable )。 我也试过如下铸造,但它不起作用(它说“预期类型”): var myContainer = genericMethodInfo.Invoke( myService, new object[] { property }) as typeof(IContainer).MakeGenericType(type); 其中type是运行时类型, myService是公开GetContainer方法的服务, property是IProperty类型。 更新 :在我的博客中查看我的完整解决方案: http : //stefanoricciardi.com/2010/02/18/generics-with-type-uknown-at-compile-time/