reflection

How do you pass parameters by ref when calling a static method using reflection?

馋奶兔 提交于 2021-02-16 06:05:44
问题 I'm calling a static method on an object using reflection: MyType.GetMethod("MyMethod", BindingFlags.Static).Invoke(null, new object[] { Parameter1, Parameter2 }); How do you pass parameters by ref, rather that by value? I assume they would be by value by default. The first parameter ("Parameter1" in the array) should be by ref, but I can't figure out how to pass it that way. 回答1: For a reference parameter (or out in C#), reflection will copy the new value into the object array at the same

Generate an instantiable class based on a static class reflectively

不打扰是莪最后的温柔 提交于 2021-02-15 07:42:12
问题 Can I (using reflection I presume?) create a class with all the same method names as java.lang.Math and each method just forwards it to Math ? E.g. public class MyMath { public MyMath() {} public double abs(double a) { return Math.abs(a); } public float abs(float a) { return Math.abs(a); } public int abs(int a) { return Math.abs(a); } ... public double acos(double a) { return Math.acos(a); } ... How could I generate this programmatically? 回答1: You can do this with reflection to get an

Generate an instantiable class based on a static class reflectively

烂漫一生 提交于 2021-02-15 07:42:07
问题 Can I (using reflection I presume?) create a class with all the same method names as java.lang.Math and each method just forwards it to Math ? E.g. public class MyMath { public MyMath() {} public double abs(double a) { return Math.abs(a); } public float abs(float a) { return Math.abs(a); } public int abs(int a) { return Math.abs(a); } ... public double acos(double a) { return Math.acos(a); } ... How could I generate this programmatically? 回答1: You can do this with reflection to get an

Cast negative number to unsigned types (ushort, uint or ulong)

别来无恙 提交于 2021-02-15 04:55:08
问题 How to cast some negative number to unsigned types . Type type = typeof (ushort); short num = -100; ushort num1 = unchecked ((ushort) num); //When type is known. Result 65436 ushort num2 = unchecked(Convert.ChangeType(num, type)); //Need here the same value 回答1: There are only 4 types. so simply you can write your own method for that. private static object CastToUnsigned(object number) { Type type = number.GetType(); unchecked { if (type == typeof(int)) return (uint)(int)number; if (type ==

C# How to use GetMethod to find a specific overload with a generic out parameter?

梦想的初衷 提交于 2021-02-11 13:48:47
问题 Everything is in the title, but let me put some code. Let's say we have a class with 2 methods with the same name: class MyClass { /// <summary> /// The first version /// </summary> public TItem Foo(long param1) { ... } /// <summary> /// Overload with a generic parameters /// </summary> public bool Foo<TItem>(out TItem item, long param1) { ... } } An we need to get the MethodInfo for the second 'Foo' method which has a generic out parameter: public class AnotherClass { public MethodInfo

C# Validate that generic type extends generic abstract class with type parameters

*爱你&永不变心* 提交于 2021-02-11 12:57:11
问题 I've am stuck and I'm looking for some clarification. I'm having an issue with reflection, I have a WPF app and I've implemented a plugin system as documented here: https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support However, I've modified and extended this a bit where my plugins extend an Abstract class instead of an Interface directly because I have some stuff in the constructor. On my project, the overall structure is the following: public abstract class

How do I get the real annotation instead of a proxy from ConstraintDescriptor::getAnnotation? (javax.validation Apache Bval)

本秂侑毒 提交于 2021-02-11 12:07:30
问题 I'm trying to get a Class object reference for the annotation which caused a ConstraintViolation in the javax.validation package (Apache Bval implementation). After getting some ConstraintViolations, I pass them into the following function: private Class<?> getConstraintAnnotation(final ConstraintViolation<?> constraintViolation) { return constraintViolation .getConstraintDescriptor() .getAnnotation() .getClass(); } And this returns a class object whose getName() , getCanonicalName() , and

How do I get the real annotation instead of a proxy from ConstraintDescriptor::getAnnotation? (javax.validation Apache Bval)

半城伤御伤魂 提交于 2021-02-11 12:07:25
问题 I'm trying to get a Class object reference for the annotation which caused a ConstraintViolation in the javax.validation package (Apache Bval implementation). After getting some ConstraintViolations, I pass them into the following function: private Class<?> getConstraintAnnotation(final ConstraintViolation<?> constraintViolation) { return constraintViolation .getConstraintDescriptor() .getAnnotation() .getClass(); } And this returns a class object whose getName() , getCanonicalName() , and

How do I get the real annotation instead of a proxy from ConstraintDescriptor::getAnnotation? (javax.validation Apache Bval)

二次信任 提交于 2021-02-11 12:04:28
问题 I'm trying to get a Class object reference for the annotation which caused a ConstraintViolation in the javax.validation package (Apache Bval implementation). After getting some ConstraintViolations, I pass them into the following function: private Class<?> getConstraintAnnotation(final ConstraintViolation<?> constraintViolation) { return constraintViolation .getConstraintDescriptor() .getAnnotation() .getClass(); } And this returns a class object whose getName() , getCanonicalName() , and

Deserialize JSON into multiple inherited classes

孤人 提交于 2021-02-11 09:22:49
问题 When I serialize my JSON object out of DocumentDB, my Control is not deserializing into the OptionsControl with the Options property. I have the following class, Control : public class Control : IControl { public Guid Id { get; set; } public virtual Enums.ControlType Type { get; set; } public string PropertyName { get; set; } public string ControlCssClass { get; set; } public string Description { get; set; } } I also have OptionsControl , which inherits from Control : public class