reflection

Converting List<Object> using a System.Type object

浪子不回头ぞ 提交于 2020-01-06 05:45:08
问题 Let's say I have retrieved a System.Type object using reflection and want to use that type to convert a List<Object> into another List of that type. If I try: Type type = GetTypeUsingReflection(); var myNewList = listObject.ConvertAll(x => Convert.ChangeType(x, type)); I get an exception since the object does not implement the IConvertible interface. Is there a way around this or another way to approach this problem? 回答1: Type type = typeof(int); // could as well be obtained by Reflection var

Why strings stored in object variables and compared with == have this strange behavior?

二次信任 提交于 2020-01-06 05:38:08
问题 Strange behavior when comparing two string properties with the usage of reflection. var a = new A { X = "aa", B = 1 }; var b = new A { X = "aa", B = 2 }; Type type = typeof(A); object aObjValue = type.GetProperty("X")?.GetValue(a); object bObjValue = type.GetProperty("X")?.GetValue(b); Console.WriteLine("aObjValue == bObjValue : " + (aObjValue == bObjValue)); Console.WriteLine("aObjValue.Equals(bObjValue) : " + aObjValue.Equals(bObjValue)); a.X = Console.ReadLine(); aObjValue = type

Why strings stored in object variables and compared with == have this strange behavior?

与世无争的帅哥 提交于 2020-01-06 05:38:05
问题 Strange behavior when comparing two string properties with the usage of reflection. var a = new A { X = "aa", B = 1 }; var b = new A { X = "aa", B = 2 }; Type type = typeof(A); object aObjValue = type.GetProperty("X")?.GetValue(a); object bObjValue = type.GetProperty("X")?.GetValue(b); Console.WriteLine("aObjValue == bObjValue : " + (aObjValue == bObjValue)); Console.WriteLine("aObjValue.Equals(bObjValue) : " + aObjValue.Equals(bObjValue)); a.X = Console.ReadLine(); aObjValue = type

C# Can't reflect into private field

柔情痞子 提交于 2020-01-06 05:25:50
问题 I got an issue. In Unity I want to reflect into a private field. But I always get null for the fieldinfo. what am I doing wrong? public abstract class _SerializableType { [SerializeField] private string name; } // because I am using a CustomPropertyDrawer for all inherited from _SerializeType public class SerializableType<T> : _SerializableType { } public class SerializableType : _SerializableType { } [Serializable] public class CTech : SerializableType<_CouplingTechnology> { } so using this

Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

Deadly 提交于 2020-01-06 04:21:25
问题 Using .Net 3.5 I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property... [Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")] public virtual double Weight{ get; set; } And I have a Validate method in the class that checks validation attributes... protected virtual void Validate() { var type = this.GetType(); foreach (var property in type.GetProperties()) { foreach (ValidationAttribute attribute in property.GetCustomAttributes(typeof(ValidationAttribute),true

Range DataAnnotation Doesn't Seem to Be Working in .Net 3.5

流过昼夜 提交于 2020-01-06 04:20:42
问题 Using .Net 3.5 I have a Range Attributes (System.ComponentModel.DataAnnotations) on a Property... [Range(0, 5, ErrorMessage = "Weight must be between 0 and 5")] public virtual double Weight{ get; set; } And I have a Validate method in the class that checks validation attributes... protected virtual void Validate() { var type = this.GetType(); foreach (var property in type.GetProperties()) { foreach (ValidationAttribute attribute in property.GetCustomAttributes(typeof(ValidationAttribute),true

Getting the System.Type from an assembly loaded at runtime

夙愿已清 提交于 2020-01-06 03:30:13
问题 As a followup to this question I have now come to the problem of being able to get the Type of a type that is defined by the user in his own solution. Using the standard mscorlib types, everything works. The question is very easy: how can I get this type from an assembly that I will only know at runtime? As described in the comments here: Also, what do you mean by "extracting the type"? You mean getting the Reflection type? There's no good helper, partly because (typically) you can never

Getting the System.Type from an assembly loaded at runtime

大兔子大兔子 提交于 2020-01-06 03:30:04
问题 As a followup to this question I have now come to the problem of being able to get the Type of a type that is defined by the user in his own solution. Using the standard mscorlib types, everything works. The question is very easy: how can I get this type from an assembly that I will only know at runtime? As described in the comments here: Also, what do you mean by "extracting the type"? You mean getting the Reflection type? There's no good helper, partly because (typically) you can never

Get annotations when exec-maven-plugin runs Main does not work

余生长醉 提交于 2020-01-06 03:16:24
问题 I would like to run a Main class with exec-maven-plugin and from my dependencies generate documentation like a swagger file. The annotation that I care is javax.ws.rs.Path which has @Retention(RetentionPolicy.RUNTIME) My Java code public class ContextClassReader extends ClassReader { private static final ExtensibleClassLoader CLASS_LOADER = new ExtensibleClassLoader(); public ContextClassReader(final String className) throws IOException { super(CLASS_LOADER.getResourceAsStream(className

In Scala, fetched value of declared field cast to its class-declared type

偶尔善良 提交于 2020-01-05 11:09:29
问题 I would like to ask how to achieve the following in Scala. Consider scala> case class C(i:Int) defined class C scala> val c = C(1) c: C = C(1) Given a field of interest, in this case scala> val fname = "i" fname: String = i we would like to retrieve the original value and type of field i in c. A first, naive, attempt included the following, scala> val f = c.getClass.getDeclaredField(fname) f: java.lang.reflect.Field = private final int C.i scala> f.setAccessible(true) scala> f.getType res3: