system.reflection

Get assembly version in PCL

Deadly 提交于 2019-11-29 17:21:04
问题 I have the following line of code in .NET 4.5 that I am trying to build as Portable Class Library. It's purpose is to get assembly version: this.GetType().Assembly.GetName().Version.Major; The problem is that Assembly.GetName() is not available in PCL. Is there a way to get assembly version in PCL? I know it is possible to parse Assembly.FullName, but I want a better solution. 回答1: public static string Version { get { var assembly = typeof(MyType).GetTypeInfo().Assembly; // In some PCL

.GetType().GetProperties() returns properties in different order

自作多情 提交于 2019-11-29 16:42:08
I want to check our configuration file and see if it is the same as if I were to create a new configuration file. This method is called GetConfig(). After some hours I noticed that if I save my configuration file and then call GetConfig it works, but if I close the program start it up and load my configuration file in and call GetConfig() it returns my properties in a different order. Below you can see what I mean, property b is an object of a class. There are more than 3 properties, but I only wanted to give a small example: - - - - - - - - -- - - - - - -- S A V E C O N F I G - - - - - - -- -

How to call custom operator with Reflection

本秂侑毒 提交于 2019-11-29 16:28:55
问题 In my small project I'm using System.Reflection classes to produce executable code. I need to call the + operator of a custom type. Does anybody know how can I call customized operator of custom class using C# reflection? 回答1: C# compiler converts overloaded operator to functions with name op_XXXX where XXXX is the operation. For example, operator + is compiled as op_Addition . Here is the full list of overloadable operators and their respective method names: ┌──────────────────────────┬─────

C# Getting Parent Assembly Name of Calling Assembly

六月ゝ 毕业季﹏ 提交于 2019-11-29 16:00:46
问题 I've got a C# unit test application that I'm working on. There are three assemblies involved - the assembly of the C# app itself, a second assembly that the app uses, and a third assembly that's used by the second one. So the calls go like this: First Assembly ------> Second Assembly---------> Third Assembly. What I need to do in the third assembly is get the name of the Fist Assembly that called the second assembly. Assembly.GetExecutingAssembly().ManifestModule.Name Assembly

GetProperty reflection results in “Ambiguous match found” on new property

只谈情不闲聊 提交于 2019-11-29 10:44:31
问题 How can I get my property? Currently an error is occuring of Ambiguous match found , see the comment line in code. public class MyBaseEntity { public MyBaseEntity MyEntity { get; set; } } public class MyDerivedEntity : MyBaseEntity { public new MyDerivedEntity MyEntity { get; set; } } private static void Main(string[] args) { MyDerivedEntity myDE = new MyDerivedEntity(); PropertyInfo propInfoSrcObj = myDE.GetType().GetProperty("MyEntity"); //-- ERROR: Ambiguous match found } 回答1: Type

Using Reflection to set a static variable value before object's initialization?

不打扰是莪最后的温柔 提交于 2019-11-29 03:01:52
Is there anyway to set the value of a static (private) variable on an object that has not been initialized? The SetValue method requires an instance, but I'm hoping there's a way to get around this. JaredPar For static values you can pass null for the instance parameter. var type = typeof(SomeClass); var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, 42); could you create a static function that is public and use it to set your private static variable ? 来源: https://stackoverflow.com/questions/2203545/using-reflection-to-set-a-static

I need an alternative to `Assembly.GetEntryAssembly()` that never returns null

心不动则不痛 提交于 2019-11-28 10:44:31
I need to find the assembly in which managed code execution started. // using System.Reflection; Assembly entryAssembly = Assembly.GetEntryAssembly(); This seems like the way to go, but the MSDN reference page for Assembly.GetEntryAssembly states that this method "[c]an return null when called from unmanaged code." In that case, I would like to know which assembly was called by unmanaged code. Is there a reliable way of doing this, i.e. one that always returns a non-null Assembly reference? The best I could think of so far is the following, which should work in a single-threaded scenario: //

how to serialize/deserialize an assembly object to and from a byte array

a 夏天 提交于 2019-11-28 04:51:58
问题 Let's say a create an (executable) assembly in memory by compiling a code string. Then I want to serialize this assembly object into a byte array and then store it in a database. Then later on I want to retrieve the byte array from the database and deserialize the byte array back into an assembly object, then invoke the entry point of the assembly. At first I just tried to do this serialization like I would any other simple object in .net, however apparently that won't work with an assembly

Load an ASP.NET 2.0 aspx page using System.Reflection?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 01:41:56
Can I load an stand alone aspx page in another stand alone aspx page using System.Reflection? I am using the ASP.NET 2.0 Web site project model. Try using BuildManager.CreateInstanceFromVirtualPath . Sample usage: Page p = BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx", typeof(Page)) This answers this specific question, although, based on your comments, I'm not sure that this is what you really want. Don't know about doing it using Reflection, which may well be possible, but you can capture the output of an aspx or asp page to a string writer using HttpContext.Server.Execute(). I

Using Reflection to set a static variable value before object's initialization?

别来无恙 提交于 2019-11-27 17:14:36
问题 Is there anyway to set the value of a static (private) variable on an object that has not been initialized? The SetValue method requires an instance, but I'm hoping there's a way to get around this. 回答1: For static values you can pass null for the instance parameter. var type = typeof(SomeClass); var field = type.GetField("SomeField", BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, 42); 回答2: could you create a static function that is public and use it to set your private