system.reflection

C# reflection use variable as object.[var]

南楼画角 提交于 2019-12-02 04:20:36
问题 I'm trying to generate a table in a razor view using reflection to pull the properties from the model. Here is what I've tried: @if (@Model.Count() > 0) { System.Reflection.PropertyInfo[] properties = Model.First().GetType().GetProperties(); <table> <thead> <tr> @foreach (var property in properties) { if (char.IsLower(property.Name.ToCharArray()[0])) //ignore foreign keys { continue; } <th>@property.Name</th> } </tr> </thead> <tbody> @foreach (PCNWeb.Models.Switch item in Model) { /*System

Get overridden property attribute

二次信任 提交于 2019-12-02 02:41:54
问题 I have a custom attribute like this: public class PropertyInfoAttribute : Attribute { public bool IsAutoComplete { get; set; } } And there is a class like this: public class Article { public virtual int Order { get; set; } //other properties } In another class,which inherits from Article, I override Order property and declare the attribute for it like this: public class ArticleDetails : Article { [PropertyInfo(IsAutoCompele = true)] public override int Order { get; set; } } The problem

Why do CanRead and CanWrite return false in C# for properties with overridden accessors?

我的未来我决定 提交于 2019-12-01 21:36:55
问题 When trying to get properties accessors from derived properties or use CanRead / CanWrite, for some reason base auto-properties are not taken into account. CanRead and CanWrite return values based only on the derived type, also GetMethod and SetMethod don't contain methods from base type. However when writing code accessors from base type can be used (so that we can read overridden auto-property with only setter defined in derived type). Here is the code to reproduce it written as an unit

Generically populate different classes members

帅比萌擦擦* 提交于 2019-12-01 13:23:16
问题 I am working on a web-service application with several (11) web-service calls. For each web-service I need to populate the Soap Body from a string array like this: if (aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString().Length != 0) { wsSoapBody.Branch = aMessage[(int)DCSSCustomerUpdate_V3.Branch].ToString(); } aMessage[int] is the string array, and [int] is defined by an enumerated constant - in this case it is defined like this: private enum DCSSCustomerUpdate_V3 { MsgType = 0,

How to get the list of methods called from a method using reflection in C#

拈花ヽ惹草 提交于 2019-11-30 20:48:46
How to get the list of methods called from a method using reflection in C# (DotNet) or How can I check whether Method1 is called from Method2 using reflection? As others have pointed out, this is essentially impossible to do using reflection. You'd have to parse the IL byte code of the methods yourself in order to find the calls. Luckily, there's a beautiful project going by the name of Mono Cecil (also available on nuget ) that does all the hard work for you. Here's a minimal example to illustrate how your problem could be solved using Mono Cecil: static class MethodDefinitionExtensions {

Get Types in assembly (error: System.Reflection.ReflectionTypeLoadException)

你离开我真会死。 提交于 2019-11-30 17:23:13
I´m receiving an Exception of type "Exception Details: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information." with the following code: public IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true) { foreach(var a in assemblies) { foreach (var t in a.GetTypes()) I need to Get the Types defined in each assembly but it seems that it cannot be generated. I already performed all typical procedures related to wrong assembly

How do I look up the internal properties of a C# class? protected? protected internal?

泄露秘密 提交于 2019-11-30 12:42:01
If I have a C# class MyClass as below: using System.Diagnostics; namespace ConsoleApplication1 { class MyClass { public int pPublic {get;set;} private int pPrivate {get;set;} internal int pInternal {get;set;} } class Program { static void Main(string[] args) { Debug.Assert(typeof(MyClass).GetProperties( System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Length == 1); Debug.Assert(typeof(MyClass).GetProperties( System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Length == 2); // internal? // protected? // protected internal? } } }

How to call custom operator with Reflection

情到浓时终转凉″ 提交于 2019-11-30 10:59:14
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? 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: ┌──────────────────────────┬───────────────────────┬──────────────────────────┐ │ Operator │ Method Name │ Description │ ├───────────────────

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

眉间皱痕 提交于 2019-11-30 09:47:21
问题 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

How to get the list of methods called from a method using reflection in C#

懵懂的女人 提交于 2019-11-30 05:39:12
问题 How to get the list of methods called from a method using reflection in C# (DotNet) or How can I check whether Method1 is called from Method2 using reflection? 回答1: As others have pointed out, this is essentially impossible to do using reflection. You'd have to parse the IL byte code of the methods yourself in order to find the calls. Luckily, there's a beautiful project going by the name of Mono Cecil (also available on nuget) that does all the hard work for you. Here's a minimal example to