reflection

How to convert objects in reflection

早过忘川 提交于 2019-12-25 09:32:26
问题 I have this piece of code which is working fine, My question is if i want to add another element to the xml named 'Allow'. the Allow Element can get the values of 'True' or 'False'. I want to convert it to a boolean property in my Plugin class, meaning the Plugin will have the additional property: public bool Allow { get; set; } is there a possibility to convert it? can you give a code example? using System; using System.Collections.Generic; using System.Linq; using System.IO; using System

Get methods and attributes from dll assembly

强颜欢笑 提交于 2019-12-25 09:19:46
问题 I'm trying to get a small plugin mechanism running by reflecting an dll file providing my class Plugin (implementing my Plugin -Interface shared among dll and main project / sorry for naming both the same) offering an attribute of type string and a main-method activate : Interface: public interface Plugin { string pluginName{get;set;} void activate(System.Windows.Forms.Form main); } dll class: public class Plugin : WhiteA.Plugin { public string pluginName{get;set;} public void activate(System

PHP Reflection with two calls

只愿长相守 提交于 2019-12-25 09:16:27
问题 I am trying to archive some reflection in php. How can i make something like that. With my code i am getting following error: Undefined property: A::$getB()->getStr() class B{ public function getStr(){ return 'str'; } } class A{ public function getB(){ return new B(); } } $a = new A(); $method = 'getB()->getStr()'; echo($a->$method); 回答1: You have to split the call chain to the single calls $getB = "getB"; $str = "getStr"; $a = new A(); echo $a->$getB()->$str(); 回答2: You should definitely use

How to compute a hash of assembly, so that this hash could be used to tell if the code in the assembly has changed?

断了今生、忘了曾经 提交于 2019-12-25 09:06:19
问题 The problem is that rebuilding exactly the same code generates a different assembly when compared with the result of the previous build. Why do I need this? I have a T4 template that generates certain source code from the given contract assembly. That source code is checked in into VCS, even though it is generated. This is because the contract assembly changes relatively infrequently. However, when it does change, I would like to fail the build as long as the aforementioned T4 template is not

java - Initial package of class from another directory

耗尽温柔 提交于 2019-12-25 09:02:05
问题 I have some problem how to initial package from class file from another directory File file=new File("D:\\java\\myproject\\Name_pack\\time\\MyClass.class"); URL[] cp = { new File(file.getParent()).toURI().toURL() }; URLClassLoader urlcl = new URLClassLoader(cp); Class cls = urlcl.loadClass(file.getName().replaceAll("\\.class",""); if class file not contain a package, it's working. but it's contains a package, i get some error like this : Exception in thread "AWT-EventQueue-0" java.lang

reflect.Set slice-of-structs value to a struct, without type assertion (because it's unknown)

吃可爱长大的小学妹 提交于 2019-12-25 08:08:44
问题 I'm creating a helper package to pop payloads from a queue. It's essential that this helper be agnostic to the struct used by the application importing it. This (no-op, just example) function will provide a single payload from the queue, of the provided type like interface{} : func One(like interface{}) interface{} { typ := reflect.TypeOf(like) one := reflect.New(typ) return one.Interface() } This function provides many payloads: func Many(num int, like interface{}) interface{} { typ :=

Compare fields of two unrelated objects

泪湿孤枕 提交于 2019-12-25 07:46:41
问题 I've got two objects of different classes that share some fields with the same name and type. These two objects are not related to each other. There's no possibility for me to create an interface or a parent class. Now I want to compare those shared fields and as far as I know this should be possible using reflection. These are the steps I've written for such a compare method: Field[] inputFields = input.getClass().getDeclaredFields(); for (Field field : inputFields ) { log.info(field.getName

Cast interface to a class?

不羁岁月 提交于 2019-12-25 07:43:37
问题 I have a cms with a page that contains forms. I want in the cms to have a dropdown with different class names that all implement a validation interface. My form handlers contain a process method which I want to validate against the selected validation class. So I have this interface: public interface IEligibilityValidation { bool IsValid(); } and for example this class public class ComplexEligibilityValidation : IEligibilityValidation { public bool IsValid() { return true; /* complex logic

Reflection type mismatch

蹲街弑〆低调 提交于 2019-12-25 07:37:24
问题 I have compiled a class at runtime which I would like to use on the fly, provided that its constructor takes one parameter package com.notmycompany; import com.mycompany.Manager; import com.mycompany.Processor; import com.mycompany.Event; public class CustomProcessor extends Processor { public CustomProcessor( Manager m) { super( m); } @Override public void process( Event evt) { // Do you own stuff System.out.println( "My Own Stuff"); } } Compilation goes fine and I can load the class right

Listing all the classes in a DLL

大憨熊 提交于 2019-12-25 07:35:05
问题 I would like to list all of the classes that are in a DLL without having to load the dependencies. I don't want to execute any functionality, I just want to find out (problematically) what classes are inside of a given DLL. Is that possible? I've tried using the assembly.GetTypes() call, but it fails because of dependencies for executing the DLL. Is there any other way to list all the public classes? 回答1: I suggest you use the mono Cecil library. This is the basic example: //Creates an