system.reflection

Why after changing a static readonly field via reflection the output of that readonly field is old?

时间秒杀一切 提交于 2019-12-04 13:43:52
Why is the "someValue" variable which is readonly (but we still can change its value via reflection) output as "10", although it actually did change to 55? static class Program { static readonly int someValue = 10; static void Main(string[] args) { Console.WriteLine(someValue); // 10 typeof(Program) .GetField("someValue", BindingFlags.Static | BindingFlags.NonPublic) .SetValue(null, 55); // change readonly field via reflection to 55 Console.WriteLine(someValue); // output in console 10, // but in visual studio debugger it shows 55 Console.ReadKey(); } } Probably just a JIT optimization to

Dapper with Attributes mapping

China☆狼群 提交于 2019-12-04 07:40:50
问题 I try to map my Id fields with the Column Attributes but for some reason this doesn't seem to work and I can't figure out why. I set up a test project to demonstrate what I am trying. First, I got my 2 entities: Entity Table1 using System.Data.Linq.Mapping; namespace DapperTestProj { public class Table1 { [Column(Name = "Table1Id")] public int Id { get; set; } public string Column1 { get; set; } public string Column2 { get; set; } public Table2 Table2 { get; set; } public Table1() { Table2 =

Reflection: How do I find and invoke a local functon in C# 7.0?

不想你离开。 提交于 2019-12-03 23:23:21
I have a private static generic method I want to call using reflection, but really I want to 'bundle' it inside of another method. C# 7.0 supports local functions so this is definitely possible. You would say "why don't you just call it directly?" but I'm using it to get the ability to use an object and System.Type in a strongly typed manner so I need to call it dynamically. This code already works if I have it as it's own private static generic method. private static void HandleResponse(object data, Type asType) { var application = typeof(Program); application .GetMethod(nameof(UseAs),

transferring one object properties values to another one

我怕爱的太早我们不能终老 提交于 2019-12-03 16:35:47
Before all, I know about AutoMapper , and I don't want to use it. Because I'm learning C# and I want to receive a deep view of it. So I'm trying to do this issue (explained below) myself. However, I'm trying to create a property copier to cope values of one type's properties to another one, if the property has the same name and type and is readable from source and writable in target. I'm using type.GetProperties() method. Sampled method is here: static void Transfer(object source, object target) { var sourceType = source.GetType(); var targetType = target.GetType(); var sourceProps =

How are CIL 'fault' clauses different from 'catch' clauses in C#?

烈酒焚心 提交于 2019-12-03 11:24:06
问题 According to the CLI standard (Partition IIA, chapter 19) and the MSDN reference page for the System.Reflection.ExceptionHandlingClauseOptions enum, there are four different kinds of exception handler blocks: catch clauses: "Catch all objects of the specified type." filter clauses: "Enter handler only if filter succeeds." finally clauses: "Handle all exceptions and normal exit." fault clauses: "Handle all exceptions but not normal exit." Given these brief explanations (cited from the CLI

How to get the name of current function? [duplicate]

泄露秘密 提交于 2019-12-03 04:30:36
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can you use reflection to find the name of the currently executing method? C# how to get the name of the current method from code For example: void foo() { Console.Write(__MYNAME__); } print: foo it's possible do it in C#? 回答1: Try this: System.Reflection.MethodBase.GetCurrentMethod().Name 回答2: You can check the stack trace using System.Diagnostics; // get call stack StackTrace stackTrace = new StackTrace(); //

How are CIL 'fault' clauses different from 'catch' clauses in C#?

拈花ヽ惹草 提交于 2019-12-03 01:50:13
According to the CLI standard (Partition IIA, chapter 19) and the MSDN reference page for the System.Reflection.ExceptionHandlingClauseOptions enum , there are four different kinds of exception handler blocks: catch clauses: "Catch all objects of the specified type." filter clauses: "Enter handler only if filter succeeds." finally clauses: "Handle all exceptions and normal exit." fault clauses: "Handle all exceptions but not normal exit." Given these brief explanations (cited from the CLI Standard, btw.), these should map to C# as follows: catch — catch (FooException) { … } filter — not

How to emit a Type in .NET Core

南楼画角 提交于 2019-12-02 18:00:54
In C#, how do I emit a new Type at runtime with .NET Core? All of the examples I can find for .NET 6 don't seem to work in .NET core (they all begin with getting the current AppDomain, which doesn't exist in .NET core any more). If possible I would appreciate an example that involves creating a Type and adding a property to the Type. Aram Kocharyan Here is SO post about creating a dynamic type in .NET 4. How to dynamically create a class in C#? And in the accepted answer is just one use of AppDomain . AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(an,

How to get the name of current function? [duplicate]

a 夏天 提交于 2019-12-02 16:59:05
Possible Duplicate: Can you use reflection to find the name of the currently executing method? C# how to get the name of the current method from code For example: void foo() { Console.Write(__MYNAME__); } print: foo it's possible do it in C#? Raphaël Althaus Try this: System.Reflection.MethodBase.GetCurrentMethod().Name Albin Sunnanbo You can check the stack trace using System.Diagnostics; // get call stack StackTrace stackTrace = new StackTrace(); // get calling method name Console.WriteLine(stackTrace.GetFrame(0).GetMethod().Name); But beware, if the method is inlined you get the parent

Dapper with Attributes mapping

北城余情 提交于 2019-12-02 15:18:39
I try to map my Id fields with the Column Attributes but for some reason this doesn't seem to work and I can't figure out why. I set up a test project to demonstrate what I am trying. First, I got my 2 entities: Entity Table1 using System.Data.Linq.Mapping; namespace DapperTestProj { public class Table1 { [Column(Name = "Table1Id")] public int Id { get; set; } public string Column1 { get; set; } public string Column2 { get; set; } public Table2 Table2 { get; set; } public Table1() { Table2 = new Table2(); } } } and entity Table2 using System.Data.Linq.Mapping; namespace DapperTestProj { public