reflection

Generate dynamic method to set a field of a struct instead of using reflection

孤者浪人 提交于 2020-01-09 09:07:31
问题 Let's say I have the following code which update a field of a struct using reflection. Since the struct instance is copied into the DynamicUpdate method, it needs to be boxed to an object before being passed. struct Person { public int id; } class Test { static void Main() { object person = RuntimeHelpers.GetObjectValue(new Person()); DynamicUpdate(person); Console.WriteLine(((Person)person).id); // print 10 } private static void DynamicUpdate(object o) { FieldInfo field = typeof(Person)

How to set the generic type of an ArrayList at runtime in java?

社会主义新天地 提交于 2020-01-09 08:13:31
问题 Ok, so I am setting up my own XML serialization (I know there are others out there, even some built in to Java, but I am doing it myself to learn and because it is so awesome to work with). I have serialization down. I am currently on the deserialization (reading in the XML file and assembling objects based on the data in the file) and I am running into problems with setting generic types. After extensive research, I figured out how to get the generic types of a class so I could write them

How to set the generic type of an ArrayList at runtime in java?

亡梦爱人 提交于 2020-01-09 08:09:08
问题 Ok, so I am setting up my own XML serialization (I know there are others out there, even some built in to Java, but I am doing it myself to learn and because it is so awesome to work with). I have serialization down. I am currently on the deserialization (reading in the XML file and assembling objects based on the data in the file) and I am running into problems with setting generic types. After extensive research, I figured out how to get the generic types of a class so I could write them

How to find all the types in an Assembly that Inherit from a Specific Type C#

☆樱花仙子☆ 提交于 2020-01-09 04:16:51
问题 How do you get a collection of all the types that inherit from a specific other type? 回答1: Something like: public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType) { return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t)); } If you need to handle generics, that gets somewhat trickier (e.g. passing in the open List<> type but expecting to get back a type which derived from List<int> ). Otherwise it's simple though :) If you want to exclude the type itself, you

Wrong number of arguments error when invoking a method

浪尽此生 提交于 2020-01-09 03:43:25
问题 I have class AClass and a method someMethod that gets an Object array as parameter. public class AClass { public void someMethod(Object[] parameters) { } } In main, when I try to invoke this method on the the object that I have created and give an object array as a parameter to this method Object[] parameters; // lets say this object array is null Class class = Class.forName("AClass"); Object anObject = class.newInstance(); Method someMethod = class.getDeclaredMethod("someMethod", parameters

Java - List cast to be able to use addAll function

点点圈 提交于 2020-01-07 09:31:34
问题 Supposed I have an entity that invokes some method Object methodVal = ety.getClass().getMethod("someMethod").invoke(ety); My goal is to cast it to List in order to user the function like addAll , so I tried List.class.cast(methodVal).addAll((Collection<?>) Objects.requireNonNull(someValue)); //someValue is an Object and I cast it to Collection<?>) The code is working fine and the app is still can run, however I'm getting a warning saying Unchecked call to 'addAll(Collection<? extends E>)' as

How can I know the classes that extend my base class at runtime? [duplicate]

偶尔善良 提交于 2020-01-07 09:06:43
问题 This question already has answers here : Get all derived types of a type (8 answers) Get all inherited classes of an abstract class [duplicate] (4 answers) Closed 5 years ago . During runtime , I would like to populate a drop down list with classes that have extended my base class. Currently I have an enum, and this is what I use to populate that list, but I want to add additional classes (and other people are adding classes) and do not want to have to maintain an enum for this purpose. I

“Safe handle has been closed” Thread abort: can program crash be avoided?

你。 提交于 2020-01-07 08:10:36
问题 Im using undetermined DLLs which can use undetermined resources such as a COM port. Some DLL methods don't have their own timeouts, so i am forced to abort the execution thread. But if the thread is using a resource such as a COM port, and i abort the thread, my program crashes with the error “Safe handle has been closed”. I know why this happens but is there any way to catch this exception or skip it, rather than an actual crash? 回答1: Solution: Running the code in a separate AppDomain

Dynamically build an object from a strongly typed class using C#?

你离开我真会死。 提交于 2020-01-07 08:09:22
问题 Currently, am adding the properties and values to the object manually like this example and sending to Dapper.SimpleCRUD to fetch data from Dapper Orm. This is the desired output I would like to achieve. object whereCriteria = null; whereCriteria = new { CountryId = 2, CountryName = "Anywhere on Earth", CountryCode = "AOE", IsActive = true }; The following class should build the object in the above mentioned format and return the ready-made object. public static class WhereClauseBuilder {

How to sort through a generic list object

风格不统一 提交于 2020-01-07 08:00:09
问题 My code is designed to parse an unknown json file using json.net that has nested classes and create checkboxes to easily look through it. At some point during the parsing it arrives to a List object I have the type and the actual object The type has AssemblyQualifiedName "System.Collections.Generic.List`1[[Calibration.Camera, CalibrationOrganizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 " The object