zoukankan
html css js c++ java
今天写的代码,可惜没有用上,但想想可能以后还能用上吧。
是关于自定义数据的简单实现,你可以这样使用。
//
define ConnectionString property.
CustomProperty
<
string
>
ConnectionStringProperty
=
new
CustomProperty
<
string
>
(
"
ConnectionStringProperty
"
);
//
create customData
CustomData data
=
new
CustomData();
data.SetValue
<
string
>
(ConnectionStringProperty,
"
test
"
);
string
v
=
data.GetValue
<
string
>
(ConnectionStringProperty);
这个代码主要还是演示如何使用
DebuggerDisplay
和
DebuggerTypeProxy
功能。
详细的代码如下:
CustomProperty
//
==========================================
//
File: CustomProperty.cs
//
Date: 2008-03-10
//
Author: tansm
//
------------------------------
//
//
Description:
//
提供自定义的选项属性键
//
//
History: 2008-03-10 Created
//
//
===========================================
using
System;
namespace
Dcms.Common.Advanced
{
/**/
///
<summary>
///
自定义选项属性的描述对象。被应用在ApplicationControlService的自定义属性描述
///
</summary>
///
<typeparam name="T">
此属性的类型,必须是值类型
</typeparam>
[
Serializable,
System.Diagnostics.DebuggerDisplay(
@"
{PropertyName}:{PropertyType}
"
)
]
public
sealed
class
CustomProperty
<
T
>
{
/**/
///
<summary>
///
创建 CustomProperty 实例
///
</summary>
///
<param name="propertyName">
属性的名称,不能为空
</param>
public
CustomProperty(
string
propertyName)
{
参数检查
#region
参数检查
if
(
string
.IsNullOrEmpty(propertyName))
{
throw
new
ArgumentNullException(
"
propertyName
"
);
}
#endregion
_propertyName
=
propertyName;
_defaultValue
=
default
(T);
_isReadonly
=
false
;
}
/**/
///
<summary>
///
创建 CustomProperty 实例
///
</summary>
///
<param name="propertyName">
属性的名称,不能为空
</param>
///
<param name="defaultValue">
属性的缺省值
</param>
///
<param name="isReadonly">
此属性是否是只读
</param>
public
CustomProperty(
string
propertyName, T defaultValue,
bool
isReadonly)
{
参数检查
#region
参数检查
if
(
string
.IsNullOrEmpty(propertyName))
{
throw
new
ArgumentNullException(
"
propertyName
"
);
}
#endregion
_propertyName
=
propertyName;
_defaultValue
=
defaultValue;
_isReadonly
=
isReadonly;
}
private
string
_propertyName;
/**/
///
<summary>
///
返回自定义属性的名称
///
</summary>
public
string
PropertyName
{
get
{
return
_propertyName; }
}
/**/
///
<summary>
///
返回属性的类型
///
</summary>
public
Type PropertyType
{
get
{
return
typeof
(T); }
}
private
T _defaultValue;
/**/
///
<summary>
///
返回此属性的默认值
///
</summary>
public
T DefaultValue
{
get
{
return
_defaultValue; }
}
private
bool
_isReadonly;
/**/
///
<summary>
///
返回属性是否是只读属性
///
</summary>
public
bool
IsReadonly
{
get
{
return
_isReadonly; }
}
/**/
///
<summary>
///
返回此对象的Hash值
///
</summary>
///
<returns>
实例的Hash值
</returns>
public
override
int
GetHashCode()
{
return
System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(
this
);
}
/**/
///
<summary>
///
判断两个对象是否相等
///
</summary>
///
<param name="obj">
要判断的实例
</param>
///
<returns>
相等返回true
</returns>
public
override
bool
Equals(
object
obj)
{
return
object
.ReferenceEquals(obj,
this
);
}
/**/
///
<summary>
///
返回此实例的字符串表示
///
</summary>
///
<returns>
实例的字符串表示
</returns>
public
override
string
ToString()
{
return
string
.Format(
"
{0}:{1}
"
, _propertyName,
typeof
(T));
}
}
}
CustomData
//
==========================================
//
File: CustomData.cs
//
Date: 2008-03-10
//
Author: tansm
//
------------------------------
//
//
Description:
//
自定义属性包装类
//
//
History: 2008-03-10 Created
//
//
===========================================
using
System;
using
System.Collections.Generic;
using
System.Diagnostics;
namespace
Dcms.Common.Advanced
{
/**/
///
<summary>
///
包含自定义属性的类,此类被应用在ApplicationControlService
///
</summary>
[
Serializable,
System.Diagnostics.DebuggerDisplay(
@"
Count = {Count}
"
),
DebuggerTypeProxy(
typeof
(CustomDataDebugView)),
]
public
class
CustomData
{
private
Dictionary
<
object
,
object
>
_values;
//
property为键
/**/
///
<summary>
///
创建 CustomData 实例
///
</summary>
public
CustomData()
{
_values
=
new
Dictionary
<
object
,
object
>
();
}
/**/
///
<summary>
///
返回指定自定义属性的值,如果没有包含值,将返回默认值
///
</summary>
///
<typeparam name="T">
属性的值类型
</typeparam>
///
<param name="property">
属性键
</param>
///
<returns>
此属性的值,如果没有找到将返回默认值
</returns>
public
T GetValue
<
T
>
(CustomProperty
<
T
>
property)
{
参数检查
#region
参数检查
if
(property
==
null
)
{
throw
new
ArgumentNullException(
"
property
"
);
}
#endregion
object
result;
if
(_values.TryGetValue(property,
out
result))
{
return
(T)result;
}
return
property.DefaultValue;
}
/**/
///
<summary>
///
设置某个属性的值,如果是只读属性只能设置一次。
///
</summary>
///
<typeparam name="T">
属性的值类型
</typeparam>
///
<param name="property">
属性键
</param>
///
<param name="value">
属性的新值
</param>
public
void
SetValue
<
T
>
(CustomProperty
<
T
>
property, T value)
{
参数检查
#region
参数检查
if
(property
==
null
)
{
throw
new
ArgumentNullException(
"
property
"
);
}
#endregion
if
(_values.ContainsKey(property))
{
if
(property.IsReadonly)
{
throw
new
ApplicationException(
string
.Format(
"
自定义属性{0}是只读属性,只能设置一次。
"
, property));
}
_values[property]
=
value;
}
else
{
_values.Add(property, value);
}
}
/**/
///
<summary>
///
返回所有的属性列表
///
</summary>
///
<returns>
属性列表
</returns>
public
object
[] GetProperties()
{
object
[] keys
=
new
object
[_values.Count];
_values.Keys.CopyTo(keys,
0
);
return
keys;
}
/**/
///
<summary>
///
返回属性的总数
///
</summary>
public
int
Count
{
get
{
return
_values.Count; }
}
/**/
///
<summary>
///
将数据复制到数组中
///
</summary>
///
<param name="array">
新的数组
</param>
///
<param name="arrayIndex">
开始索引
</param>
internal
void
CopyTo(KeyValuePair
<
object
,
object
>
[] array,
int
arrayIndex)
{
((ICollection
<
KeyValuePair
<
object
,
object
>>
)_values).CopyTo(array, arrayIndex);
}
}
internal
sealed
class
CustomDataDebugView
{
private
CustomData _customData;
public
CustomDataDebugView(CustomData customData)
{
参数检查
#region
参数检查
if
(customData
==
null
)
{
throw
new
ArgumentNullException(
"
customData
"
);
}
#endregion
this
._customData
=
customData;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public
KeyValuePair
<
object
,
object
>
[] Items
{
get
{
KeyValuePair
<
object
,
object
>
[] values
=
new
KeyValuePair
<
object
,
object
>
[_customData.Count];
_customData.CopyTo(values,
0
);
return
values;
}
}
}
}
查看全文
相关阅读:
【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档(转
PHP Client for Mysql Binlog
MySQL的binlog日志恢复(转)
Linux 普通进程 后台进程 守护进程(转)
实战:MySQL Sending data导致查询很慢的问题详细分析(转)
mysql索引无效且sending data耗时巨大原因分析
阿里云-DRDS(转)
MySQL查看SQL语句执行效率(转)
nginx php-fpm 输出php错误日志(转)
Golang指针基本介绍及使用案例
原文地址:https://www.cnblogs.com/tansm/p/1099151.html
最新文章
java中获取类加载路径和项目根路径的5种方法
分支限界法
测试的概念
k-means聚类学习
LDA基本介绍以及LDA源码分析(BLEI)
主题模型
毕业论文一次性修改所有字母和数字的字体
关于移动端的钓鱼式攻击
怎样在居中公式的同时,编号右对齐
Git学习笔记五--分支管理
热门文章
未来WEB程序员
404页面 3秒后跳到首页 实现
常用的CSS命名规则
基于HTML5+CSS3的图片旋转、无限滚动、文字跳动特效
中文Ubuntu系统根目录文件夹名称变为英文
永不消逝的电波(二)HackRF入门:家用无线门铃信号重放
Ubuntu使用Remastersys封装制作系统ISO镜像
传教士与野人过河问题
Ubuntu安装Osmocom-BB一只猿多频点WEB脚本
整理了一份招PHP高级工程师的面试题(转)
Copyright © 2011-2022 走看看