C# Extend class by adding properties

雅博官网在线注册雅博体育锁定账户 This question already has an answer here: Does C# have extension properties? 6 answers First of all, you should probably reconsider your approach. But if all else fails, here is how you can sort of add a property to a sealed class: using System; using System.Runtime.CompilerServices; namespace DataCellExtender { #region sample 3rd party class public class DataCell { p

C#通过添加属性来扩展类

这个问题在这里已经有了答案: C#有扩展属性吗? 6个答案 首先,你应该重新考虑你的方法。 但是,如果所有其他都失败了,这里是你如何可以添加一个属性到一个密封的类: using System; using System.Runtime.CompilerServices; namespace DataCellExtender { #region sample 3rd party class public class DataCell { public int Field1; public int Field2; } #endregion pu

dotnet core : Can not find assembly file Microsoft.CSharp.dll

I have a project that i have not run for some while, build with dotnet core 1.1.2 dependencies. in the meanwhile I have updated visual studio, possible installed some dotnet core stuff for 2.0 and my application do not run anymore. InvalidOperationException: Can not find assembly file Microsoft.CSharp.dll at 'C:devEarthMLEarthML.MapifysrcEarthML.Mapify.PortalbinDebugnet462win10-x64refs,C:devEa

dotnet核心:无法找到程序集文件Microsoft.CSharp.dll

我有一个项目,我有一段时间没有运行,建立与dotnet核心1.1.2依赖项。 在此期间,我更新了Visual Studio,可能为2.0安装了一些dotnet核心内容,而我的应用程序不再运行。 InvalidOperationException: Can not find assembly file Microsoft.CSharp.dll at 'C:devEarthMLEarthML.MapifysrcEarthML.Mapify.PortalbinDebugnet462win10-x64refs,C:devEarthMLEarthML.MapifysrcEarthML.Mapify.PortalbinDebugnet462win10-x64' Micro

Is it acceptable to use extension methods on a class which you can modify

I've recently been toying with the idea of using extension methods to implement helper utilities on classes which I control (ie, are in the same program and I can modify). The rationale behind it is that many times, these helper utilities are used in very specific scenarios and don't require access to the classes internal values. For instance, let's say I have a StackExchange class

在可以修改的类上使用扩展方法是可以接受的

我最近一直在玩弄使用扩展方法在我控制的类上实现助手实用程序的想法(即,在同一个程序中,我可以修改)。 背后的基本原理是很多时候,这些帮助程序实用程序都用于特定的场景,并且不需要访问类内部值。 例如,假设我有一个StackExchange类。 它会有PostQuestion和Search和AnswerQuestion 。 现在,如果我想手动计算我的声望以确保StackOverflow不会欺骗我,该怎么办? 我会按照以下方式执行一些操作: int rep=0; forea

C# Extension Methods Architecture Question

I recently asked this question: Compiler error referencing custom C# extension method Marc Gravell answer was perfect and it solved my problem. But it gave me something to think about... If and Extension method must be placed on a Static Class and the method itself must be static, why can't we create a static Extension method? I understand that the parameter marked as "this"

C#扩展方法体系结构问题

我最近问了这个问题:编译器错误引用了自定义的C#扩展方法 Marc Gravell的回答很完美,它解决了我的问题。 但它给了我一些想法...... 如果和扩展方法必须放在静态类上,并且方法本身必须是静态的,为什么我们不能创建静态扩展方法? 我明白标记为“this”的参数将被用来允许访问我们正在扩展的对象的一个??实例。 我不明白的是为什么不能创建一个静态的方法......在我看来,这是一个毫无意义的限制...... 我的问题是:

Mixing generic methods and extension methods

I created the Class1.GetChild() where T : DependencyObject extension method in lib1.dll assembly. After that, all assemblies that depends on lib1.dll failed to compile with error: The type 'System.Windows.DependencyObject' is defined in an assemebly that is not referenced. You must add a reference to assembly 'WindowsBase' etc... Why dependent assemblies requires Wi

混合通用方法和扩展方法

我在Class1.GetChild() where T : DependencyObject创建了Class1.GetChild() where T : DependencyObject扩展方法。 之后,所有依赖lib1.dll的程序集都无法编译时出现错误: 类型'System.Windows.DependencyObject'被定义为可引用的,未被引用。 您必须添加对程序集“WindowsBase”等的引用... 为什么依赖程序集需要WindowsBase,即使它们不使用GetChild ? 。 重现(vs2010 .net4): lib1

C#, WinForms and Extension Methods

The Question Aside from all the obvious answers, what would cause extension methods to generate compiler errors like this one: 'DataType' does not contain a definition for 'YourExtensionMethodName' I've got a real stumper here, and it's spelled out for you in detail below. I've exhausted all the possible causes I can think of. Scenario I have a couple of ext

C#,WinForms和扩展方法

问题 除了所有明显的答案之外,什么会导致扩展方法产生像这样的编译器错误: 'DataType'不包含'YourExtensionMethodName'的定义 我在这里有一个真正的击球手,并在下面详细说明。 我已经用尽了所有我能想到的可能原因。 脚本 我在WinForms应用程序使用的DLL中的各种静态类中定义了几个扩展方法。 扩展方法签名与我正在扩展的类中的方法的签名不冲突(在本例中为String )。 DLL和WinForms应用程

In C#, what happens when you call an extension method on a null object?

Does the method get called with a null value or does it give a null reference exception? MyObject myObject = null; myObject.MyExtensionMethod(); // <-- is this a null reference exception? If this is the case I will never need to check my 'this' parameter for null? That will work fine (no exception). Extension methods don't use virtual calls (ie it uses the "call" il

在C#中,当您在空对象上调用扩展方法时会发生什么?

该方法是否使用空值调用,或者是否给出空引用异常? MyObject myObject = null; myObject.MyExtensionMethod(); // <-- is this a null reference exception? 如果是这种情况,我将永远不需要检查我的'this'参数为null? 这将工作正常(没有例外)。 扩展方法不使用虚拟调用(即它使用“call”il指令,而不是“callvirt”),所以没有空检查,除非你自己写在扩展方法中。 这在少数情况下非常有用: public static b

Creating a circularly linked list in C#?

What would be the best way to create a circularly linked list in C#. Should I derive it from the LinkedList< T> collection? I'm planning on creating a simple address book using this Linked List to store my contacts (it's gonna be a suck-y address book, but I don't care cause I'll be the only one to use it). I mainly just want to create the crucially linked list so that I

在C#中创建一个循环链表?

在C#中创建循环链表的最佳方式是什么? 我应该从LinkedList 集合中派生吗? 我打算使用这个链接列表来创建一个简单的地址簿来存储我的联系人(这将会是一个糟糕的地址簿,但我不在乎,因为我将是唯一使用它的人)。 我主要只是想创建关键链表,以便我可以在其他项目中再次使用它。 如果您不认为链接列表是正确的方式,请告诉我哪种方式会更好。 由于这些答案大部分并未真正解决问题的实质,仅仅是意图,或许这

copying property values from one instance to another, different classes

I have two C# classes that have many of the same properties (by name and type). I want to be able to copy all non-null values from an instance of Defect into an instance of DefectViewModel . I was hoping to do it with reflection, using GetType().GetProperties() . I tried the following: var defect = new Defect(); var defectViewModel = new DefectViewModel(); PropertyInfo[] defectProperties = d

将属性值从一个实例复制到另一个实例,不同的类

我有两个C#类有许多相同的属性(按名称和类型)。 我希望能够将Defect实例中的所有非空值复制到DefectViewModel实例中。 我希望通过使用GetType().GetProperties()来反射。 我尝试了以下内容: var defect = new Defect(); var defectViewModel = new DefectViewModel(); PropertyInfo[] defectProperties = defect.GetType().GetProperties(); IEnumerable viewModelPropertyNames = defectViewModel.Ge

size format provider

Is there any easy way to create a class that uses IFormatProvider that writes out a user-friendly file-size? public static string GetFileSizeString(string filePath) { FileInfo info = new FileInfo(@"c:windowsnotepad.exe"); long size = info.Length; string sizeString = size.ToString(FileSizeFormatProvider); // This is where the class does its magic... } It should result in strings form

大小格式提供者

有没有简单的方法来创建一个使用IFormatProvider写出用户友好的文件大小的类? public static string GetFileSizeString(string filePath) { FileInfo info = new FileInfo(@"c:windowsnotepad.exe"); long size = info.Length; string sizeString = size.ToString(FileSizeFormatProvider); // This is where the class does its magic... } 它应该导致字符串格式化为“2.5 MB”,“3,9 GB”,“670字节”等。 我使用