clr

Canonical: How to call .NET methods from Excel VBA

被刻印的时光 ゝ 提交于 2019-11-26 06:39:36
问题 I have found a way to call .NET 2 code directly from a VBA macro: Dim clr As mscoree.CorRuntimeHost Set clr = New mscoree.CorRuntimeHost clr.Start Dim domain As mscorlib.AppDomain clr.GetDefaultDomain domain Dim myInstanceOfDotNetClass As Object Set myInstanceOfDotNetClass = domain.CreateInstanceFrom(\"SomeDotNetAssembly.dll\", \"Namespace.Typename\").Unwrap Call myInstanceOfDotNetClass.ExecuteSomeDotNetMethod (To make this code work I had to add references to mscoree.tlb and mscorlib.tlb to

What's the method representation in memory?

无人久伴 提交于 2019-11-26 06:36:09
问题 While thinking a little bit about programming in Java/C# I wondered about how methods which belong to objects are represented in memory and how this fact does concern multi threading. Is a method instantiated for each object in memory seperately or do all objects of the same type share one instance of the method? If the latter, how does the executing thread know which object\'s attributes to use? Is it possible to modify the code of a method in C# with reflection for one, and only one object

Why check this != null?

别来无恙 提交于 2019-11-26 06:31:39
问题 Occasionally I like to spend some time looking at the .NET code just to see how things are implemented behind the scenes. I stumbled upon this gem while looking at the String.Equals method via Reflector. C# [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)] public override bool Equals(object obj) { string strB = obj as string; if ((strB == null) && (this != null)) { return false; } return EqualsHelper(this, strB); } IL .method public hidebysig virtual instance bool Equals

Why C# is not allowing non-member functions like C++

橙三吉。 提交于 2019-11-26 06:06:33
问题 C# will not allow to write non-member functions and every method should be part of a class. I was thinking this as a restriction in all CLI languages. But I was wrong and I found that C++/CLI supports non-member functions. When it is compiled, compiler will make the method as member of some unnamed class. Here is what C++/CLI standard says, [Note: Non-member functions are treated by the CLI as members of some unnamed class; however, in C++/CLI source code, such functions cannot be qualified

LINQ on the .NET 2.0 Runtime

不问归期 提交于 2019-11-26 04:34:31
Can a LINQ enabled app run on a machine that only has the .NET 2.0 runtime installed? In theory, LINQ is nothing more than syntactic sugar, and the resulting IL code should look the same as it would have in .NET 2.0. How can I write LINQ without using the .NET 3.5 libraries? Will it run on .NET 2.0? There are some "Hacks" that involve using a System.Core.dll from the 3.5 Framework to make it run with .net 2.0, but personally I would not want use such a somewhat shaky foundation. See here: LINQ support on .NET 2.0 Create a new console application Keep only System and System.Core as referenced

Static Generic Class as Dictionary

允我心安 提交于 2019-11-26 04:27:08
问题 A static field in a generic class will have a separate value for each combination of generic parameters. It can therefore be used as a Dictionary<Type, whatever > Is this better or worse than a static Dictionary<Type, whatever >? In other words, which of these implementations is more efficient? public static class MethodGen<TParam> { public static readonly Action<TParam> Method = CreateMethod(); static Action<TParam> CreateMethod() { /*...*/ } } Or, public static class MethodGen { static

How do I detect at runtime that .NET version 4.5 is currently running your code?

人走茶凉 提交于 2019-11-26 04:08:00
问题 I installed .NET 4.5 Developer preview from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=27541, which \'replaces\' .NET 4.0 version. However, the old way to detect the .NET framework version seems to return 4.0 (more precisely 4.0.30319.17020 on my PC), instead of 4.5 (sure probably for backward compatibility, or?): using System; namespace ConsoleApplication { class Program { static void Main(string[] args) { var version = Environment.Version; Console.WriteLine(version

Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? [duplicate]

走远了吗. 提交于 2019-11-26 03:39:51
问题 This question already has an answer here: Inheritance on a constrained generic type parameter 3 answers Why cannot C# generics derive from one of the generic type parameters like they can in C++ templates? I mean I know it impossible because CLR does not support this, but why? I am aware of the profound differences between C++ templates and C# generics - the former are compile time entities and must be resolved during the compilation, while the latter are first class run-time entities. Still,

C# &#39;is&#39; operator performance

梦想的初衷 提交于 2019-11-26 03:34:04
问题 I have a program that requires fast performance. Within one of its inner loops, I need to test the type of an object to see whether it inherits from a certain interface. One way to do this would be with the CLR\'s built-in type-checking functionality. The most elegant method there probably being the \'is\' keyword: if (obj is ISpecialType) Another approach would be to give the base class my own virtual GetType() function which returns a pre-defined enum value (in my case, actually, i only

Performance surprise with “as” and nullable types

跟風遠走 提交于 2019-11-26 02:33:57
I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I thought this was really neat, and that it could improve performance over the C# 1 equivalent, using "is" followed by a cast - after all, this way we only need to ask for dynamic type checking once, and then a simple value check. This appears not to be the case, however. I've included a sample test app below, which basically sums all the integers within