reflection

Mocking objects without no-argument constructor in C# / .NET

微笑、不失礼 提交于 2020-01-03 06:44:11
问题 Is it possible to create a mock from a class that doesn't provide a no-argument constructor and don't pass any arguments to the constructor? Maybe with creating IL dynamically? The background is that I don't want to define interfaces only for testing. The workaround would be to provide a no-argument constructor for testing. 回答1: Sure thing. In this example i'll use Moq, a really awesome mocking library. Example: public class MyObject { public MyObject(object A, object B, object C) { // Assign

Thread.Abort() method freezes

泪湿孤枕 提交于 2020-01-03 05:40:10
问题 So I've googled that it freezes because of using unsafe code, and AbortException throws only when control flow returns to managed code. So, in my case I have a native library, called in a thread. So sometimes I can't abort it, because the library is native and the Abort method not just do nothing, but freezes the calling thread. So, I'd like to solve it. For example, using a different process should help, but it's very complicated. So, a less heavy solution is to use ' AppDomains' . But

Reflecting methods to clear Android app cache

一世执手 提交于 2020-01-03 05:32:28
问题 I am trying to clear the app cache of other android apps besides my own. To do this, I am using reflection on the PackageManager class. However, whenever I initialize the method before I invoke it, it always ends up being null. private void initiateClearUserData() { // Invoke uninstall or clear user data based on sysPackage String thePackageName; PackageManager pm = speedy.this.getPackageManager(); List<ApplicationInfo> installedApps = pm.getInstalledApplications(0); ApplicationInfo ai;// =

Reflectively accessing final static variable without initialization

主宰稳场 提交于 2020-01-03 04:39:06
问题 I am trying to access a bunch of final static public ints from a class using reflection. This class however doesn't have a constructor - e.g. the android R.id . I am trying to get all int values for these, however I can't seem to access it due to the fact that you can't create the class. I was thinking of possible extending it just to create a constructor, but I am unsure this is wise. Any suggestions? I can't modify R.id or R.array (at least I shouldn't I think). Thanks in advanced! Jon 回答1:

Exception when access inner class reflectively

老子叫甜甜 提交于 2020-01-03 03:33:42
问题 Here is a sample program tested in Java 1.5. I wonder why the two approaches below have different result. Is it a bug or a kind of Java feature? package test; public class TestOut { public static void main(String[] args) { // works new TestIn(); // throws IllegalAccessException Class.forName("test.TestOut$TestIn").newInstance(); } private static class TestIn { } } 回答1: The class is private, hence the IllegalAccessException - you can use: Class cls = Class.forName(...); Constructor c = cls

Log function calls parameter values & return values in c#

為{幸葍}努か 提交于 2020-01-03 02:47:12
问题 Say I have a class that looks like this. public static class Config { public static string GetAppSetting(string key) { return ConfigurationManager.AppSettings[key].ToString(); } } And I wanted to log every call to this method along with the key parameter & return value. The only code change I want to make is this: [Log] public static class Config { public static string GetAppSetting(string key) { return ConfigurationManager.AppSettings[key].ToString(); } } I'll most likely use log4net to log

How best to keep a cached list of member fields, one each for a family of case classes in Scala

自作多情 提交于 2020-01-03 02:08:05
问题 This is a follow up to the following question: Fastest way to get the names of the fields of a case class in Scala I'm trying to find a simple way to provide fast custom serialization (lets say to a list of tuples of (String, Object) , which can be converted into a db row in production or an in memory map in unit testing) to a family of case classes in Scala, and it seems that keeping a cached list of a fields of the class may be a promising way of doing this. However, I'm not sure about the

C# Compile at Runtime Visual Studio unusual behavior

懵懂的女人 提交于 2020-01-02 23:05:30
问题 Helo, i have a strange behavior in Visual Studio. My code is a compiling at runtime, in VS it workts fine but if i start the programm from release-folder the compiled dll seems to be blocked, what is not that unusal but why does VS dont say that? Can some one tell me also how to unload a compiled dll in runtime? VS: Release-Folder: My Test Code here: static void Main(string[] args) { var property = "Value"; var key = "MyKey"; var binding = string.Format("{0}.{1}", key, property); var dllName

Nested fully qualified property name using reflection

佐手、 提交于 2020-01-02 21:47:34
问题 I have the following classes: public class Car { public Engine Engine { get; set; } public int Year { get; set; } } public class Engine { public int HorsePower { get; set; } public int Torque { get; set; } } I'm getting all the nested properties using this: var result = typeof(Car).GetProperties(BindingFlags.Public | BindingFlags.Instance).SelectMany(GetProperties).ToList(); private static IEnumerable<PropertyInfo> GetProperties(PropertyInfo propertyInfo) { if (propertyInfo.PropertyType

Nested fully qualified property name using reflection

假如想象 提交于 2020-01-02 21:46:12
问题 I have the following classes: public class Car { public Engine Engine { get; set; } public int Year { get; set; } } public class Engine { public int HorsePower { get; set; } public int Torque { get; set; } } I'm getting all the nested properties using this: var result = typeof(Car).GetProperties(BindingFlags.Public | BindingFlags.Instance).SelectMany(GetProperties).ToList(); private static IEnumerable<PropertyInfo> GetProperties(PropertyInfo propertyInfo) { if (propertyInfo.PropertyType