system.reflection

Why does sealed override of a property in C# copy not overriden accessor from base type?

若如初见. 提交于 2019-12-10 15:12:39
问题 In C# overriding auto-property and providing only one accessor makes reflection via PropertyInfo "lose" the other one, even though it is defined in base class. It can look strange at a first glance, but seems to be reasonable after more detailed analysis. However, changing override to sealed override also changes this behavior and allows to get all accessors: using System.Reflection; using NUnit.Framework; [TestFixture] public class PropertySealedOverrideReflectionTests { public class Base {

Portable Class Library equivalent to MethodBase.GetCurrentMethod

左心房为你撑大大i 提交于 2019-12-10 13:32:24
问题 s there Portable Class Library equivalent to MethodBase.GetCurrentMethod? I'm new to PCLs. I'm justing looking into whether I can use a PCL to hold some client code that will definitely be used on Silverlight and may be used elsewhere. Having scanned the source, I can see plenty of calls to MethodBase.GetCurrentMethod which doesn't seem to exist in the PCL. ** EDIT ** I've ripped this sample out of the library in question. IsNullOrEmpty() was using String.IsNullOrWhiteSpace(String) which

System.Attribute.GetCustomAttribute in .NET Core

时光怂恿深爱的人放手 提交于 2019-12-10 13:12:38
问题 I'm trying to convert the following method (which works fine in .NET Framework 4.6.2) to .NET Core 1.1. public static TAttribute GetCustomAttribute<TAttribute>(MemberInfo member) where TAttribute : Attribute { var attr = System.Attribute.GetCustomAttribute(member, typeof(TAttribute)); if (attr is TAttribute) return attr; else return null; } This code is giving me the error Attribute does not contain a definition for GetCustomAttribute. Any idea what the .NET Core equivalent of this should be?

typeof(DateTime?).Name == Nullable`1

别等时光非礼了梦想. 提交于 2019-12-10 03:46:56
问题 Using Reflection in .Net typeof(DateTime?).Name returns "Nullable`1". Is there any way to return the actual type as a string. (in this case "DateTime" or "System.DateTime") I understand that DateTime? is Nullable<DateTime> . That's besides the point, I am just looking for the type of the nullable type. 回答1: There's a Nullable.GetUnderlyingType method which can help you in this case. Likely you'll end up wanting to make your own utility method because (I'm assuming) you'll be using both

CreateType missing from TypeBuilder. How to port this?

半城伤御伤魂 提交于 2019-12-10 01:47:48
问题 Trying to port an application from .net 4.5 to .net core for a client. I'm noticing that CreateType is no longer part of TypeBuilder. I've searched through multiple of the new reflection libs with no luck. Does anyone know how to port this? Code in question: typeBuilder.CreateType() 回答1: I found the answer, but in a different repository than I expected. CreateType was dropped, and CreateTypeInfo should be used now per this: https://github.com/dotnet/coreclr/issues/2222 'TypeBuilder' does not

Accessing System.Reflection.Pointer from python

这一生的挚爱 提交于 2019-12-09 06:53:25
I'm using pythonnet to access functions from a managed dll. One of the functions in the dll should return a float pointer (float*). When i call this function with pythonnet, it returns a System.Reflection.Pointer. Anyone an idea how I could get the actual data (an array of floats) from this pointer in python? Loading dll: import clr clr.AddReference(r"C:\Program Files\Thorlabs\Thorlabs OSA\ThorlabsOSAWrapper.dll") from ThorlabsOSAWrapper import * Calling function from dll: # x_values should be a float* but i get a System.Reflection.Pointer x_values = spectrum.GetXArray() 来源: https:/

Get the type of an array object [duplicate]

拈花ヽ惹草 提交于 2019-12-08 06:46:23
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: How do I use reflection to determine the nested type of an array? I have a class A and trying to get the underlying type of an array in it. Class A { A1[] obja1; A2[] obja2; string x; int i; } How do I get the underlying object type of obja1 as A1 and obja2 as A2?Here is the part of the code I have: object AClass = myAssembly.CreateInstance("A"); PropertyInfo[] pinfos = AClass.GetType().GetProperties();

Accessing System.Reflection.Pointer from python

China☆狼群 提交于 2019-12-08 06:35:42
问题 I'm using pythonnet to access functions from a managed dll. One of the functions in the dll should return a float pointer (float*). When i call this function with pythonnet, it returns a System.Reflection.Pointer. Anyone an idea how I could get the actual data (an array of floats) from this pointer in python? Loading dll: import clr clr.AddReference(r"C:\Program Files\Thorlabs\Thorlabs OSA\ThorlabsOSAWrapper.dll") from ThorlabsOSAWrapper import * Calling function from dll: # x_values should

Reflection and autogenerated types

☆樱花仙子☆ 提交于 2019-12-08 00:31:23
问题 I have a class with a single method that uses a "yield" return statement. A nested type is automatically created. Using reflection with binding flags set to BindingFlags.DeclaredOnly , I get this output: // Public members from my class. Test.FileSystemObject..ctor Test.FileSystemObject.GetFiles(DirectoryInfo directory) Test.FileSystemObject.GetFiles(String path) // Auto generated nested class. Test.FileSystemObject+<GetFiles>d__4..ctor Test.FileSystemObject+<GetFiles>d__4.<>3__directory Test

Reflection exclude all attributes from base class and specific attribute from all other derived classes

馋奶兔 提交于 2019-12-06 21:42:50
I have the following base, middle and derived classes below:: public class Base { [DataMemberAttribute()] public int ValueBase { get; set; } [IgnoreForAllAttribute("Param1", "Param2")] public int IgnoreBase { get; set; } } public class Middle : Base { [DataMemberAttribute()] public int ValueMiddle { get; set; } [IgnoreForAllAttribute("Param1", "Param2")] public int IgnoreMiddle { get; set; } } public class MostDerived : Middle { [DataMemberAttribute()] public int ValueMostDerived { get; set; } [IgnoreForAllAttribute("Param1", "Param2")] public int IgnoreMostDerived { get; set; } } I need a