reflection

Activator.CreateInstance MissingMethodException

放肆的年华 提交于 2019-12-24 02:05:09
问题 I have a class in a c# dll with the following class public class RequiredTask : Base.BaseObject { public string Name { get; set; } public string Description { get; set; } public RequiredTask() : base() { } } Which inherits from this class public class BaseObject : IBaseObject, INotifyPropertyChanged { public DateTime? UpdatedOn { get; set; } public string UpdatedBy public DateTime? CreatedOn public string CreatedBy public BaseObject() { } } Then the UI, is a VB.Net Winform, this form is going

How to do typeof of a module in a fsx file?

老子叫甜甜 提交于 2019-12-24 01:58:09
问题 Let's say I have a Foo.fsx script that contains this code: module Bar = let foobar = "foo"+"bar" open Bar let a = System.Reflection.Assembly.GetExecutingAssembly() let ty = a.GetType("Foo.Bar") // but it returns null here How can I achieve that? Thanks! 回答1: This is a tricky question, because F# interactive compiles all types into types with some mangled names (and the executing assembly can contain multiple versions of the same type). I managed to do it using a simple trick - you'd add an

Filling out Object Properties with default values Recursive

泪湿孤枕 提交于 2019-12-24 01:54:26
问题 I want to populate the object's properties with some dummy data. Here is my code but it always returns null. private static object InsertDummyValues(object obj) { if (obj != null) { var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public); foreach (var property in properties) { if (property.PropertyType == typeof (String)) { property.SetValue(obj, property.Name.ToString(), null); } else if (property.PropertyType == typeof(Boolean))

how to change the attribute value using reflection

丶灬走出姿态 提交于 2019-12-24 01:43:39
问题 is it possible to change the value of an attribute of a class using reflection. below is my class :- public class LoggerManager { private static LoggerManager _instance = new LoggerManager(); private LoggerManager() { } public static LoggerManager getInstance() { return _instance; } public Logger getLogger(String FQCN) { Logger logger = Logger.getLogger(FQCN); logger.setLevel(Level.INFO); return logger; } } i want to change the value of _instance variable using reflection.. basically i want

Calling a constructor through reflection in scala 2.10

坚强是说给别人听的谎言 提交于 2019-12-24 01:27:08
问题 What's the best practice for calling a constructor of a class in scala 2.10 (M4+) ? 回答1: Answering my own question: Calling the constructor is different than invoking a method. Here's the right way to do it in scala 2.10 import reflect.runtime.universe._ import reflect.runtime.currentMirror val typ = typeOf[Range] val constructor = typ.members.find(_.kind == "constructor").get.asMethodSymbol currentMirror reflectClass typ.typeSymbol.asClassSymbol reflectConstructor constructor apply (1,10,1)

Java Reflection: Determining class of java.util.regex.Pattern in JRuby

删除回忆录丶 提交于 2019-12-24 01:21:57
问题 Working on a legacy JRuby app (1.6.8) running under Java 11, I traced a peculiar error a test case: "require 'java'; puts java::util::regex::Pattern.class" errors ArgumentError: wrong number of arguments (0 for 1) . For other built-in JRE classes, this seems to work fine (see below). This causes JRuby itself to fail sometimes, at this line in https://github.com/jruby/jruby/blob/1.6.8/lib/ruby/site_ruby/shared/builtin/javasupport/core_ext/object.rb#L10 : if self.class.superclass.instance

Copy properties between objects using reflection and extesnion method

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:14:46
问题 This is my code where I create a "copy" of one object (Entity) into a custom object. It copies just properties with the same name in both source and target. My problem is when an Entity has a navgiaton to another Entity, for this case I added a custom attribute that I add above the property in the custom class. For example the custom class looks like: public class CourseModel:BaseDataItemModel { public int CourseNumber { get; set; } public string Name { get; set; } LecturerModel lecturer;

Java Reflect/AOP override supertype private Method

给你一囗甜甜゛ 提交于 2019-12-24 01:07:48
问题 Is it at all possible to "override" a private method of a super class in Java? The class whose method I wish to override is a third party class so I cannot modify the source. It would be ideal if there were some way to reflectively set a method on a class. Alternatively, if it is possible to intercept a private method of a third party class then this would be suitable. 回答1: Is it at all possible to "override" a private method of a super class in Java? No I don't think using Reflection there

Reflection on IQueryable OfType<>

若如初见. 提交于 2019-12-24 00:55:16
问题 I have a Employees DbSet in my Entity Framework context that can be queried as: IQueryable employees = _context.Employees; The Idea is to execute the below method using Reflection: var result= _context.Employees.OfType<PaidEmployee>() I have extended the Employee object to create an PaidEmployee class. I want to query the context for PaidEmployee using REFLECTION. Assembly asm = Assembly.LoadFrom("MyModel.dll"); Type t = asm.GetType("PaidEmployee"); var ofType = typeof(Queryable).GetMethod(

How can I dynamically create a new .java file from a Java program?

ⅰ亾dé卋堺 提交于 2019-12-24 00:49:53
问题 I want to create a one .java from the Java program. When I run the program, automatically one Java file will created in my project, and also create some run time (dynamic) variable in that file. How can I do this? I know for this I have to use a Reflection API like Class and Method , but what are the methods in Class and Method to do this? 回答1: You cannot create new classes or methods using the reflection APIs. They are not designed for this. (The Class and Method APIs are for performing