reflection

How can I retrieve all custom attributes on a class

放肆的年华 提交于 2019-12-25 07:20:10
问题 I'm trying to retrieve a list all attributes applied to my class. I can see the Attribute.GetCustomAttributes() series of methods, but I can only see methods to retrieve all attributes for assemblies, modules, members, and properties - nothing for a class. The versions of the method that take a Type as an argument are not appropriate because they only return attributes of that type, whereas I want to iterate through all the attributes in order. Why am I doing this? I'm extending my

Figure the parameter to pass of the invocation of a method via reflection

牧云@^-^@ 提交于 2019-12-25 06:56:29
问题 I have a set of classes that implement generic interface such as: public inteface CustomGenerator { Object generate(Reader r); } These classes can process the parameter accordingly and generate the response in the expected type. I have a base class that I need to populate. To populate class I use these custome generator classes and reflection. public static void copy(CustomClass target, String tag, Reader reader) { Object input = CustomGenerators.get(tag).generate(reader); String setter =

Use InvocationHandler and Proxy without using interface [duplicate]

二次信任 提交于 2019-12-25 06:39:55
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Dynamic proxy for concrete classes Once I create a InvocationHandler implementation, I can use it with Proxy.newProxyInstance function to get a object behaving differently. In this case, I have to create a interface and its implementation class to use the Proxy.newProxyInstance function: MyInterface objDest = Proxy.newProxyInstance(MyInterfaceImpl.class.getClassLoader(), MyInterfaceImpl.class.getInterfaces(),

Java getMethod() results in NoSuchMethodException error

怎甘沉沦 提交于 2019-12-25 06:34:36
问题 I am trying to use reflection to launch the appropriate method when the user inputs a string command. For instance, if the user inputs "go" in the terminal, the go() method of the Player class will be called by the process() method of the Command class. However, I cannot get my code working and I get a NoSuchMethodException error that I do not know how to fix. The lines at the source of the problem are half-way through the Command class (complete classes reproduced at the bottom): try {

Serialize Base Class Implementation for all “new” Properties

て烟熏妆下的殇ゞ 提交于 2019-12-25 06:10:06
问题 I have a bunch of classes that were auto-generated from an XSD, and provided to me in a compiled DLL for which I don't have the source code. I have a need to add interfaces to each of the types, which resulted in code such as the following: public interface IBar { string SomeProperty { get; set; } } public interface IFoo<TBar> where TBar : IBar { TBar Bar { get; set; } } public class BarWrapper : BarFromXSD, IBar { } public class FooWrapper : FooFromXSD, IFoo<BarWrapper> { [XmlElement("bar")]

Serialize Base Class Implementation for all “new” Properties

夙愿已清 提交于 2019-12-25 06:08:23
问题 I have a bunch of classes that were auto-generated from an XSD, and provided to me in a compiled DLL for which I don't have the source code. I have a need to add interfaces to each of the types, which resulted in code such as the following: public interface IBar { string SomeProperty { get; set; } } public interface IFoo<TBar> where TBar : IBar { TBar Bar { get; set; } } public class BarWrapper : BarFromXSD, IBar { } public class FooWrapper : FooFromXSD, IFoo<BarWrapper> { [XmlElement("bar")]

Invoke member through reflection passing derived class as argument

大城市里の小女人 提交于 2019-12-25 06:00:38
问题 I'm trying to call the method 'MyMethod' of class 'CMyClass'. This method has a parameter of type "CBaseClass", and I'm passing an object of type "CDerivedClass". Class CBaseClass Public m_AMember As String Sub MethodOne() // DoSomething End Sub End Class Class CDerivedClass Inherits CBaseClass Public m_MyMember As Integer Sub MethodTwo() // DoSomething End Sub End Class Class CMyClass Sub MyMethod(ByVal obj As CBaseClass) // DoSomething End Sub End Class Sub Main() 'load assembly Dim

A class that exposes different methods depending on whether an optional module is loaded

孤街醉人 提交于 2019-12-25 05:24:06
问题 I have a library consisting of multiple modules: core guava The core module is mandatory, while guava is optional. There are other optional modules (this question represents a minimal testcase). Each module exposes a set of methods that the user can invoke: class CoreVerifier { MapVerifier verify(Map); } class GuavaVerifier { MultimapVerifier verify(Multimap); } What I want Provide users a class that exports all the methods in a single place: class UnifiedVerifier { MapVerifier verify(Map);

Identifying generic type of class using reflection

隐身守侯 提交于 2019-12-25 05:23:06
问题 Is there any way to detect the type specified in a generic parameter on a class? For example, I have the three classes below: public class Customer { } public class Repository<T> { } public class CustomerRepository : Repository<Customer> { } public class Program { public void Example() { var types = Assembly.GetAssembly(typeof(Repository<>)).GetTypes(); //types contains Repository, and CustomerRepository //for CustomerRepository, I want to extract the generic (in this case, Customer) } } For

C# using generic method with Type [duplicate]

久未见 提交于 2019-12-25 04:50:50
问题 This question already has answers here : How do I use reflection to call a generic method? (7 answers) Closed 5 years ago . I'm using .Net framework 2.0 to try and do the following: I've an external service which returns a list of int. In turn I use each int to find a corresponding Type which has an Attribute with a property, key; the value of that property matches the search parameter. Using the Type t I'd like to call a generic method, but I'm unable to do this. As I will only know the Type