编写C#里面的属性代码是机械又重复的劳动, 长的变量名还难免敲错, 于是写了一个, 输入类型和名称自动生成属性, 并保存到剪贴板里, 在VS里面粘贴过来就行了.
## generate cs property## string name## ==>## ///# ///# /// # public string Name# {# get { return _name; }# set { _name = value; }# } private string _name;#use Win32::Clipboard;###sub usage(){ print("Invalid input\n"); print("Input format:\n"); print(" Example: string Name\n");}## #sub MakeProperty($$){ ($type,$name) = @_; $member_name = get_member_name( $name ); $property_name = get_property_name( $name ); $s = "#region $property_name\n"; $s .= "/// \n"; $s .= "/// \n"; $s .= "/// \n"; $s .= "public $type $property_name\n"; $s .= "{\n"; $s .= "\tget\n"; $s .= "\t{\n"; if( uc($type) eq "STRING" ) { $s .= "\t\tif ($member_name == null)\n"; $s .= "\t\t{\n"; $s .= "\t\t\t$member_name = string.Empty;\n"; $s .= "\t\t}\n"; } else { if( is_value_type($type) == 0 ) { $s .= "\t\tif ($member_name == null)\n"; $s .= "\t\t{\n"; $s .= "\t\t\t$member_name = new $type();\n"; $s .= "\t\t}\n"; } } $s .= "\t\treturn $member_name;\n"; $s .= "\t}\n"; # set $s .= "\tset\n"; $s .= "\t{\n"; $s .= "\t\t$member_name = value;\n"; $s .= "\t}\n"; $s .= "} private $type $member_name;\n"; $s .= "#endregion //$property_name\n"; $s .= "\n"; output($s);}## param is a value type, example: int int32 float ...#sub is_value_type($){ @values = qw(bool boolean byte sbyte char int16 int32 int64 uint16 uint32 uint64 int float single long double decimal); ($t)= @_; $t = uc($t); foreach $item( @values ) { if ( $t eq uc($item) ) { return 1; } } return 0;}###sub open_output_file(){ open( $filehandle, ">output.cs") or die "open output file fail!"; return $filehandle;}###sub close_output_file(){ close( $filehandle );}###sub output($){ print $filehandle $s;}###sub get_property_name( $ ){ ($val) = @_; return ucfirst($val);}###sub get_member_name($){ ($val) = @_; $m = '_' . lcfirst($val); return $m;}## main#sub main(){ while(<>) { @list = split(" ", $_); if( @list>=2 ) { $type = $list[0]; $name = $list[1]; MakeProperty($type, $name); print "output property: $name\n"; } else { if( @list == 1) { $s = substr($list[0],0,1); if( $s eq 'q' || $s eq 'Q') { quit(); } } usage(); } }}###sub quit(){ close_output_file(); copy_file_content(); exit;}###sub copy_file_content(){ open( $filehandle, "output.cs" ) or die "open output.cs fail"; @array = <$filehandle>; foreach( @array ) { $lines = $lines . $_; } $CLIP = Win32::Clipboard(); $CLIP->Set($lines); close($filehandle); print "Already copy to clipboard.\n";}## main#open_output_file();main();quit();