reflection

Using a generic type of any nested subclass within its abstract superclass

人盡茶涼 提交于 2020-02-23 07:04:10
问题 Suppose you have the following abstract java class: public abstract class AbstractRequestHandler<I,O> { I input; O output; } and the following child classes hierarchy: public abstract class AbstractUserRequestHandler<I extends User,O> extends AbstractRequestHandler<I,O>{...} public abstract class AbstractUniversityRequestHandler<I extends UniversityUser> extends AbstractUserRequestHandler<I,String>{...} public class StudentRequestHandler extends AbstractUniversityRequestHandler<Student>{...}

How do I get a discriminated union case from a string?

南楼画角 提交于 2020-02-22 06:11:46
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

How do I get a discriminated union case from a string?

对着背影说爱祢 提交于 2020-02-22 06:08:12
问题 I have a discriminated union and I want to select a case based on a string (which is read from a JSON file). This is easy to do: type MyDU = A | B let str = "B" let myDU : MyDU = match str with | "A" -> MyDU.A | "B" -> MyDU.B | _ -> failwith "whatever" // val myDU : MyDU = B However, sometimes there are many cases, which would require a lot of typing. The Microsoft.FSharp.Reflection library allows me to get a UnionCaseInfo object: open Microsoft.FSharp.Reflection let myDUInfo : UnionCaseInfo

Detect if a generic type is open?

柔情痞子 提交于 2020-02-21 13:31:28
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

谁说胖子不能爱 提交于 2020-02-21 13:26:32
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

断了今生、忘了曾经 提交于 2020-02-21 13:26:14
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

心已入冬 提交于 2020-02-21 13:26:06
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

橙三吉。 提交于 2020-02-21 13:25:51
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

How do I use .Net reflection to search for a property by name ignoring case?

…衆ロ難τιáo~ 提交于 2020-02-20 08:47:10
问题 I had the following line snippet of code that searches for a propery of an instance by name: var prop = Backend.GetType().GetProperty(fieldName); Now I want to ignore the case of fieldName, so I tried the following: var prop = Backend.GetType().GetProperty(fieldName, BindingFlags.IgnoreCase); ... No dice. Now prop won't find field names that have the exact case. Hence..... How do I use .Net reflection to search for a property by name ignoring case? 回答1: You need to specify BindingFlags.Public

CreateDelegate with unknown types

∥☆過路亽.° 提交于 2020-02-10 09:01:06
问题 I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main<T> and a method which looks like this: Delegate.CreateDelegate(typeof(Func<T, object>), get) where get is a MethodInfo of the property that should be read. The problem is that when the property returns int (I guess this happens for value types) the above code throws ArgumentException because the method cannot be bound. In case of string it works well. To solve the