reflection

How to create a Func<> delegate programmatically

孤者浪人 提交于 2020-01-02 08:07:09
问题 I have a small dependency injection framework, and I am trying to make it resolve Lazy<> instances dynamically. The idea is to do something like that: DIContainer.Register<IDbCommand,SqlCommand>(); var lazyCommand = DIContainer.Resolve<Lazy<IDbCommand>>(); I read the other day that Autofac was able of doing that. I am stuck trying to set the constructor for that Lazy<> instance. In the next test code, a exception is thrown because the desired type constructor is expecting a Func<arg> , but I

c# Recursive Reflection & Generic Lists setting default properties

为君一笑 提交于 2020-01-02 06:54:12
问题 I am trying to to use reflection to achieve the following: I need a method where i pass in an object and this method will recursively instantiate the object with child objects and set the properties with default values. I need the entire object instantiated going as many levels as needed. this method needs to be able to handle an object with a multiple properties that will be generic lists of other objects. Here is my sample code (I am getting a parameter count mismatch exception when i get

c# property override Set method

痞子三分冷 提交于 2020-01-02 06:51:05
问题 I have a class like the below, I want to override the set value of "School,Country..etc.." property when some one sets a value , i don't want to change the student class but i need to do it in the base class and use it as a generic method public class Student : BaseClass { public String School { get; set; } public String Country{ get; set; } } ie: When some one sets Student.School="Harvard" , I need to store it as Student.School="Harvard my custom value" ; Note: Basically calling

Get Namespace, classname from a dll in C# 2.0

坚强是说给别人听的谎言 提交于 2020-01-02 05:48:07
问题 I will get dll's dynamically. I need to load the dll and get the namespace, classname to invoke a method (the method name is static it will be always "OnStart()"). Basically I need to run a method by loading the dll. Can somebody help!!!. 回答1: To load the assembly, you would do this: Assembly assembly = Assembly.LoadFile(@"test.dll"); This assumes you have the assemblies on disk as files. If you don't, like if you get them from a database as a byte array, there are other methods on the

Calling a method using reflection

我怕爱的太早我们不能终老 提交于 2020-01-02 05:23:26
问题 Is it possible to call a method by reflection from a class? class MyObject { ... //some methods public void fce() { //call another method of this object via reflection? } } Thank you. 回答1: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main { public static void main(final String[] argv) { final Main main; main = new Main(); main.foo(); } public void foo() { final Class clazz; final Method method; clazz = Main.class; try { method = clazz

How to use a dynamically generated object as the data source of CodeEffects generator

邮差的信 提交于 2020-01-02 05:21:51
问题 We are using this component www.codeeffects.com which allows us to create business rules based on the object properties. The html of the view is like this: @{ ViewBag.Title = "Post Example"; Html.CodeEffects().Styles() .SetTheme(ThemeType.Gray) .Render(); } @using (Html.BeginForm("Evaluate", "Post", FormMethod.Post)) { <div class="area"> <h1 class="title">Post Example</h1> <div style="margin-top:10px;"> <span>Info:</span> <span style="color:Red;">@ViewBag.Message</span> </div> <div style=

Cast to concrete class and call method in Java

二次信任 提交于 2020-01-02 03:56:08
问题 Lets say we have a baseclass called A and some subclasses ( B , C , D , etc.). Most subclasses have the method do() but the baseclass does not. Class AA provides a method called getObject() , which will create an object of type B , or C or D , etc., but returns the object as type A . How do I cast the returned object to the concrete type and call its do() method, if this method is available? EDIT: I'm not allowed to change the implementation of Class A , the subclasses or AA , since im using

In Scala, how to create a TypeTag from a type that is serializable? [duplicate]

别来无恙 提交于 2020-01-02 03:39:12
问题 This question already has answers here : How to create a TypeTag manually? (4 answers) Closed 9 days ago . In Scala reflection, the TypeTag can usually be constructed from a Type using a TypeCreator: object TypeUtils { import ScalaReflection.universe._ def createTypeTag[T]( tpe: Type, mirror: reflect.api.Mirror[reflect.runtime.universe.type] ): TypeTag[T] = { TypeTag.apply( mirror, NaiveTypeCreator(tpe) ) } case class NaiveTypeCreator(tpe: Type) extends reflect.api.TypeCreator { def apply[U <

PHP get static methods

亡梦爱人 提交于 2020-01-02 03:37:09
问题 i want to call a class method by a var (like this): $var = "read"; $params = array(...); //some parameter if(/* MyClass has the static method $var */) { echo MyClass::$var($params); } elseif (/* MyClass hat a non-static method $var */) { $cl = new MyClass($params); echo $cl->$var(); } else throw new Exception(); i read in the php-manual how to get the function-members of a class (get_class_methods). but i get always every member without information if its static or not. how can i determine a

Type-safe method reflection in Java

心已入冬 提交于 2020-01-02 03:34:05
问题 Is any practical way to reference a method on a class in a type-safe manner? A basic example is if I wanted to create something like the following utility function: public Result validateField(Object data, String fieldName, ValidationOptions options) { ... } In order to call it, I would have to do: validateField(data, "phoneNumber", options); Which forces me to either use a magic string, or declare a constant somewhere with that string. I'm pretty sure there's no way to get around that with