C# 通过添加属性扩展类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17616239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Extend class by adding properties
提问by Vans S
Is it possible in C# to extend a class not by adding only functions but properties. Ex: i have a standard DLL library I am relying on and the vendor does not want to modify it.
在 C# 中是否可以通过仅添加函数而是通过属性来扩展类。例如:我有一个我依赖的标准 DLL 库,供应商不想修改它。
Already throughout the code I have used the DataCell class extensively and only now realized that I need to add an extra property to it, as creating a new extension class that inherits from this class just does not look like it would work + a lot of rewriting.
已经在整个代码中我广泛使用了 DataCell 类,现在才意识到我需要向它添加一个额外的属性,因为创建一个从该类继承的新扩展类看起来不起作用 + 大量重写.
DataCell [metadata]
数据单元 [元数据]
public class DataCell : Message
{
public int Field1;
public int Field2;
public DataCell()
{
..
}
..
}
Basically I want to add a public int Flags; to this class. So I can do now without rewriting anything, (new DataCell).Flags = 0x10;
基本上我想添加一个公共 int Flags;到这个班。所以我现在可以不用重写任何东西了,(new DataCell).Flags = 0x10;
采纳答案by dss539
First of all, you should probably reconsider your approach. But if all else fails, here is how you can sort ofadd a property to a sealed class:
首先,您可能应该重新考虑您的方法。但是,如果所有其他方法都失败了,您可以通过以下方式向密封类添加属性:
using System;
using System.Runtime.CompilerServices;
namespace DataCellExtender
{
#region sample 3rd party class
public class DataCell
{
public int Field1;
public int Field2;
}
#endregion
public static class DataCellExtension
{
//ConditionalWeakTable is available in .NET 4.0+
//if you use an older .NET, you have to create your own CWT implementation (good luck with that!)
static readonly ConditionalWeakTable<DataCell, IntObject> Flags = new ConditionalWeakTable<DataCell, IntObject>();
public static int GetFlags(this DataCell dataCell) { return Flags.GetOrCreateValue(dataCell).Value; }
public static void SetFlags(this DataCell dataCell, int newFlags) { Flags.GetOrCreateValue(dataCell).Value = newFlags; }
class IntObject
{
public int Value;
}
}
class Program
{
static void Main(string[] args)
{
var dc = new DataCell();
dc.SetFlags(42);
var flags = dc.GetFlags();
Console.WriteLine(flags);
}
}
}
Please don't do this unless you really must. Future maintainers of this code may have some strong words for you if there's a cleaner solution that you skipped in favor of this slightly hacky approach.
请不要这样做,除非你真的必须这样做。如果您跳过了一个更简洁的解决方案来支持这种稍微有点hacky 的方法,那么此代码的未来维护者可能会对您说一些强硬的话。
回答by Jon Skeet
Well you can certainly extend a class and only add fields/properties to it (although I'd discourage the use of public fields as per your sample). However, unless other code usesyour new class, the fields won't exist in the objects created. For example, if other code has:
好吧,您当然可以扩展一个类,并且只向它添加字段/属性(尽管根据您的示例,我不鼓励使用公共字段)。但是,除非其他代码使用您的新类,否则创建的对象中将不存在这些字段。例如,如果其他代码有:
DataCell cell = new DataCell();
then that won'thave your Field1and Field2fields.
那么不会有你的Field1和Field2字段。
If everyinstance of the base class really should have these fields, you'd be better off working out how to change the base class rather than extending it.
如果基类的每个实例都应该有这些字段,那么您最好弄清楚如何更改基类而不是扩展它。
If you were wondering whether you could add "extension fields" in the same way as extension methodsare added (e.g. public static void Foo(this DataCell cell)) then no, that's not possible.
如果您想知道是否可以以与添加扩展方法相同的方式添加“扩展字段” (例如public static void Foo(this DataCell cell)),那么不,这是不可能的。
回答by jonni
There are two ways to add properties to an existing class
有两种方法可以向现有类添加属性
Add partial class, but this won't work for you because partial classes should be in the same assembly.
Inherit this classin a different class which as far I know would be a better solution for you.
添加分部类,但这对您不起作用,因为分部类应该在同一个程序集中。
在不同的类中继承这个类,据我所知,这对你来说是一个更好的解决方案。
And no you can't use a extension property like an extension method.
不,您不能像扩展方法一样使用扩展属性。

