C# 添加属性的默认实现
我上了以下课程:C# 添加属性的默认实现,c#,properties,C#,Properties,我上了以下课程: public class Foo { public string Property1 { get { return (string)Properties["property1"]; } set { Properties.Add("property1", value); } } public string Property2 { get { return (string)Properties[
public class Foo
{
public string Property1
{
get { return (string)Properties["property1"]; }
set { Properties.Add("property1", value); }
}
public string Property2
{
get { return (string)Properties["property2"]; }
set { Properties.Add("property2", value); }
}
//A lot more here
public string Property30
{
get { return (string)Properties["property30"]; }
set { Properties.Add("property30", value); }
}
public string Property31
{
get { return (string)Properties["property31"]; }
set { Properties.Add("property31", value); }
}
public Dictionary<string, object> Properties { get; set; }
}
公共类Foo
{
公共字符串属性1
{
获取{返回(字符串)属性[“property1”];}
设置{Properties.Add(“property1”,value);}
}
公共字符串属性2
{
获取{返回(字符串)属性[“property2”];}
设置{Properties.Add(“property2”,value);}
}
//这里还有很多
公共字符串属性30
{
获取{返回(字符串)属性[“property30”];}
设置{Properties.Add(“property30”,value);}
}
公共字符串属性31
{
获取{返回(字符串)属性[“property31”];}
设置{Properties.Add(“property31”,value);}
}
公共字典属性{get;set;}
}
我多次复制getter和setter,这有点麻烦,因为类有大约20/30个属性。有没有一种方法可以自动实现这一点。我通过使Foo动态化并实现TryGetMember
实现了一个对象,但我不喜欢对象不再是强类型的(尤其是visualstudio中没有autocomplete)
干杯如果您使用的是Visual Studio,则可以使用。默认情况下,这些属性可用。 您还可以使用“工具/代码段管理器”导入或导入由其他开发人员或第三方制作的代码段 这是您的代码片段的大致外观:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>myprop</Title>
<Shortcut>myprop</Shortcut>
<Description></Description>
<Author>Me</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return ($type$)Properties["$field$"];}
set { Properties.Add("$field$",value);}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
myprop
myprop
我
膨胀
类型
属性类型
int
财产
属性名
我的财产
领域
支持此属性的变量
迈瓦尔
我将使用发布一个替代方案。对于这个例子,免费版本可以很好地工作。首先从nuget安装PostSharp,然后创建简单方面:
[Serializable]
public class FooAspect : LocationInterceptionAspect {
public override void OnGetValue(LocationInterceptionArgs args) {
var foo = ((Foo)args.Instance);
if (foo.Properties == null || !foo.Properties.ContainsKey(args.LocationName))
args.Value = null;
else
args.Value = foo.Properties[args.LocationName];
}
public override void OnSetValue(LocationInterceptionArgs args) {
var foo = ((Foo) args.Instance);
if (foo.Properties == null)
foo.Properties = new Dictionary<string, object>();
foo.Properties[args.LocationName] = args.Value;
}
}
当然,仅为此使用PostSharp是一种过分的做法,但您可能会发现它在许多其他情况下也很有用。您也可以使用面向方面的编程(例如PostSharp)。它可以像你说的那样自动为你完成所有这些。啊,酷。我已经在使用PostSharp进行一些日志记录,所以我要尝试一下:)关于方面,我似乎还有很多东西需要学习。谢谢,是的,我最后也为它创建了一个片段。只是我不喜欢在源代码中反复编写类似的实现。如果我更改了实现,我必须反射我的所有属性。
[FooAspect(AttributeTargetMembers = "Property*")]
public class Foo
{
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property30 { get; set; }
public string Property31 { get; set; }
public Dictionary<string, object> Properties {get; set; }
}