• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

利用c#反射提高设计灵活性

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

反射提供了封装程序集、模块和类型的对象(Type 类型)。可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。

有了反射,即可对每一个类型了如指掌。如果获得了构造函数的信息,即可直接创建对象,即使这个对象的类型在编译时还不知道。 然而要适量应用反射,它对性能有一定影响,反射的性能损失主要来源于比较类型、遍历成员、调用成员三种情形,其中比较类型耗时最小,调用成员耗时最多,所以尽量减少采用成员动态调用等反射方式可以提高应用程序性能。除此之外,采取后期绑定、避免将反射方法放到循环内产生放大效应等办法均可提升反射性能。

 

我们可以把属性比较繁琐的控件的属性组织为xml形式,解析xml利用反射为控件付属性。把xml保存到数据库或文件,这样我们就可以不修改代码改变控件。下边以mschart为例。

属性xml:

View Code
<?xml version="1.0" encoding="gb2312"?>
<Chart Note="图表">
  <Chart.Style Note="样式">
    <Setter Property="BorderlineDashStyle" Value="Solid" Note="图标边框样式[NotSet:不设置线型;Dash:虚线;Solid:实线]"/>
    <Setter Property="BorderWidth" Value="1" Note="边框宽度"/>
    <Setter Property="BorderColor" Value="0,220,220,220" Note="边框颜色"/>
    <Setter Property="BackColor" Value="255,211,211,210" Note="背景颜色"/>
    <Setter Property="BackGradientStyle" Value="TopBottom" Note="背景渐变样式[None:不应用渐变;LeftRight:左右方向;TopBottom:上下;Center:从中心向外应用;DiagonalLeft:沿对角方向从左向右应用;DiagonalRight:沿对角方向从右向左应用; HorizontalCenter:沿水平方向从中心向外应用;VerticalCenter:沿垂直方向从中心向外应用]"/>
    <Setter Property="BackSecondaryColor" Value="255,255,249,254" Note="背景辅助颜色"/>
  </Chart.Style>
  <Chart.BorderSkin Note="边框样式">
    <Setter Property="SkinStyle" Value="Emboss" Note="边框外观样式[None:不使用;Emboss:浮雕;Raised:凸起;Sunken:凹陷;FrameThin1:圆角1;FrameTitle1:圆角2]"/>
    <Setter Property="PageColor" Value="255,255,255,255" Note="页面背景颜色"/>
  </Chart.BorderSkin>
  <Chart.Title Enabled="false" Note="标题">
    <Setter Property="Text" Value="XML线形Demo" Note="标题文字"/>
    <Setter Property="Docking" Value="Top" Note="停靠位置[Top;Bttom;Left;Right]"/>
    <Setter Property="ForeColor" Value="255,255,255" Note="字体颜色"/>
    <Setter Property="TextOrientation" Value="Horizontal" Note="文本方向[Horizontal:水平;Rotated90:文本旋转:90 度,成为自上向下的方向;Rotated270:文本旋转 270 度,成为自下向上的方向;Stacked:文本字符不旋转,只是逐个上下排列]"/>
    <Font Note="标题文字样式">
      <Setter Property="Name" Value="黑体" Note="字体"/>
      <Setter Property="Size" Value="12" Note="字体大小"/>
      <Setter Property="Blod" Value="false" Note="是否采用粗体"/>
    </Font>
  </Chart.Title>
  <Chart.ChartArea Note="区域集合" Menu="Add" AddType="ChartArea">
    <ChartArea Note="区域1" ItemName="Item1" Menu="Delete">
      <CahwtArea.Attribute Note="属性">
        <Setter Property="Name" Value="Area1" Note="名称"/>
        <Setter Property="AlignWithChartArea" Value="Area1" Note="该区域要对齐的区域名称"/>
        <Setter Property="BorderDashStyle" Value="Solid" Note="图标边框样式[NotSet:不设置线型;Dash:虚线;Solid:实线]"/>
        <Setter Property="BorderWidth" Value="1" Note="边框宽度"/>
        <Setter Property="BorderColor" Value="220,241,244,233" Note="边框颜色"/>
        <Setter Property="BackColor" Value="255,255,255,255" Note="背景颜色"/>
        <Setter Property="BackGradientStyle" Value="TopBottom" Note="背景渐变样式[None:不应用渐变;LeftRight:左右方向;TopBottom:上下;Center:从中心向外应用;DiagonalLeft:沿对角方向从左向右应用;DiagonalRight:沿对角方向从右向左应用;HorizontalCenter:沿水平方向从中心向外应用;VerticalCenter:沿垂直方向从中心向外应用]"/>
        <Setter Property="BackSecondaryColor" Value="255,255,255,255" Note="背景辅助颜色"/>
      </CahwtArea.Attribute>
      <ChartArea.Position Note="位置">
        <Setter Property="X" Value="0" Note="原点X(%)"/>
        <Setter Property="Y" Value="3" Note="原点Y(%)"/>
        <Setter Property="Width" Value="97" Note="宽度(%)"/>
        <Setter Property="Height" Value="95" Note="高度(%)"/>
      </ChartArea.Position>
      <ChartArea.Axis Note="坐标轴">
        <Axis Name="AxisX" Enabled="True" Note="AxisX" ItemName="Item1">
          <Axis.Attribute Note="坐标轴属性">
            <Setter Property="LineColor" Value="118,118,118" Note="线颜色"/>
            <Setter Property="LineDashStyle" Value="Solid" Note="线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
            <Setter Property="LineWidth" Value="1" Note="线宽"/>
            <Setter Property="ArrowStyle" Value="None" Note="箭头样式[None:没有用于相关轴的箭头;Triangle:三角形状的箭头用于相关轴;SharpTriangle:尖三角形状的箭头用于相关轴;Lines:线条形箭头用于相关轴]"/>
            <Setter Property="IsLogarithmic" Value="False" Note="是否对数坐标"/>
            <Setter Property="LogarithmBase" Value="10" Note="对数基数"/>
            <Setter Property="IsInterlaced" Value="False" Note="是否启用带状线"/>
            <Setter Property="InterlacedColor" Value="100,100,100" Note="带状线间隔颜色"/>
            <Setter Property="Minimum" Value="" Note="最小值"/>
            <Setter Property="Maximum" Value="" Note="最大值"/>
            <Setter Property="Interval" Value="" Note="间隔值"/>
            <Setter Property="IsLabelAutoFit" Value="True" Note="是否自动调整轴标签"/>
            <Setter Property="IntervalAutoMode" Value="FixedCount" Note="间隔标志[FixedCount:始终在轴上创建固定的间隔数;VariableCount:轴间隔数取决于轴长度]"/>
            <Setter Property="IsMarginVisible" Value="True" Note="是否向轴添加边距"/>
          </Axis.Attribute>
          <Axis.Title Enabled="False" Note="坐标轴文字">
            <Setter Property="Title" Value="坐标轴说明" Note="坐标轴文字内容"/>
            <Setter Property="TitleForeColor" Value="255,100,100,100" Note="坐标轴文字颜色"/>
            <Setter Property="TextOrientation" Value="Rotated90" Note="文本方向[Horizontal:水平;Rotated90:文本旋转 90 度,成为自上向下的方向;Rotated270:文本旋转 270 度,成为自下向上的方向;Stacked:文本字符不旋转,只是逐个上下排列]"/>
            <TitleFont Note="标题文字样式">
              <Setter Property="Name" Value="黑体" Note="字体"/>
              <Setter Property="Size" Value="16" Note="字体大小"/>
              <Setter Property="Blod" Value="True" Note="是否采用粗体"/>
            </TitleFont>
          </Axis.Title>
          <Axis.Grid Note="网格线">
            <Grid Name="MajorGrid" Enabled="True" Note="主要网格线" ItemName="Item1">
              <Setter Property="LineDashStyle" Value="Solid" Note="网格线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
              <Setter Property="LineWidth" Value="1" Note="线宽度"/>
              <Setter Property="LineColor" Value="210,210,210,210" Note="线颜色"/>
              <Setter Property="Interval" Value="" Note="间隔值"/>
            </Grid>
            <Grid Name="MinorGrid" Enabled="False" Note="次要网格线" ItemName="Item2">
              <Setter Property="LineDashStyle" Value="Solid" Note="网格线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
              <Setter Property="LineWidth" Value="1" Note="线宽度"/>
              <Setter Property="LineColor" Value="64,64,64" Note="线颜色"/>
              <Setter Property="Interval" Value="" Note="间隔值"/>
            </Grid>
          </Axis.Grid>
          <Axis.LabelStyle Enabled="True" Note="标签">
            <Setter Property="ForeColor" Value="195,0,0,0" Note="字体颜色"/>
            <Setter Property="Angle" Value="0" Note="绘制字体所用角度"/>
            <Setter Property="Format" Value="MM-dd" Note="文字格式"/>
            <Setter Property="IsEndLabelVisible" Value="True" Note="是否显示最后一个坐标"/>
            <Setter Property="Interval" Value="" Note="标签间隔值"/>
            <Setter Property="IsStaggered" Value="True" Note="是否显示为带有偏移量"/>
            <Setter Property="IntervalOffset" Value="0" Note="间隔的偏移量"/>
            <Font Note="字体">
              <Setter Property="Name" Value="黑体" Note="字体"/>
              <Setter Property="Size" Value="6" Note="字体大小"/>
              <Setter Property="Blod" Value="False" Note="是否采用粗体"/>
            </Font>
          </Axis.LabelStyle>
          <Axis.TickMark Note="刻度线属性">
            <TickMark Name="MajorTickMark" Enabled="True" Note="主要刻度线属性" ItemName="Item1">
              <Setter Property="Size" Value="0" Note="相对长度"/>
              <Setter Property="LineWidth" Value="1" Note="线宽"/>
              <Setter Property="LineColor" Value="200,200,200" Note="网格线颜色"/>
              <Setter Property="TickMarkStyle" Value="OutsideArea" Note="刻度线位置[None:刻度线被禁用;OutsideArea:刻度线是在图表区域外部绘制的;InsideArea:刻度线是在图表区域内部绘制的;AcrossAxis:刻度线是跨相关轴线绘制的]"/>
              <Setter Property="LineDashStyle" Value="Solid" Note="刻度线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
              <Setter Property="Interval" Value="" Note="主要网格线与次要网格线间隔值"/>
            </TickMark>
            <TickMark Name="MinorTickMark" Enabled="False" Note="次要刻度线属性" ItemName="Item2">
              <Setter Property="Size" Value="5" Note="相对长度"/>
              <Setter Property="LineColor" Value="100,100,100" Note="网格线颜色"/>
              <Setter Property="TickMarkStyle" Value="OutsideArea" Note="刻度线位置[None:刻度线被禁用;OutsideArea:刻度线是在图表区域外部绘制的;InsideArea:刻度线是在图表区域内部绘制的;AcrossAxis:刻度线是跨相关轴线绘制的]"/>
              <Setter Property="LineDashStyle" Value="Solid" Note="刻度线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
            </TickMark>
          </Axis.TickMark>
          <Axis.ScaleView Note="视图">
            <Setter Property="Zoomable" Value="true" Note="是否启用了缩放用户界面"/>
            <Setter Property="SmallScrollSizeType" Value="Days" Note="点击度量单位[Auto:自动确定;Number:数字形式;Years:以年为单位;Months:以月为单位;Weeks:以周为单位;Days:以天为单位;Hours:小时为单位;Minutes:以分钟为单位;Seconds:以秒为单位;Milliseconds:以毫秒为单位;NotSet:不会产生任何效果]"/>
            <Setter Property="SmallScrollSize" Value="1" Note="拖动小滚动尺寸"/>
            <Setter Property="SmallScrollMinSizeType" Value="Hours" Note="拖动度量单位[Auto:自动确定;Number:数字形式;Years:以年为单位;Months:以月为单位;Weeks:以周为单位;Days:以天为单位;Hours:小时为单位;Minutes:以分钟为单位;Seconds:以秒为单位;Milliseconds:以毫秒为单位;NotSet:不会产生任何效果]"/>
            <Setter Property="SmallScrollMinSize" Value="1" Note="拖动小滚动尺寸"/>
          </Axis.ScaleView>
        </Axis>
        <Axis Name="AxisX2" Enabled="True" Note="AxisX2" ItemName="Item2">
          <Axis.Attribute Note="坐标轴属性">
            <Setter Property="LineColor" Value="118,118,118" Note="线颜色[255,255,255,255]"/>
            <Setter Property="LineDashStyle" Value="Solid" Note="线样式[NotSet:不设置线型;Dash:虚线;DashDot:由重复的点划线图案构成的直线;DashDotDot:由重复的双点划线图案构成的直线;Dot:由重复的点图案构成的直线;Solid:实线]"/>
            <Setter Property="LineWidth" Value="1" Note="线宽"/>
            <Setter Property="ArrowStyle" Value="None" Note="箭头样式[None:没有用于相关轴的箭头;Triangle:三角形状的箭头用于相关轴;SharpTriangle:尖三角形状的箭头用于相关轴;Lines:线条形箭头用于相关轴]"/>
            <Setter Property="IsLogarithmic" Value="False" Note="是否对数坐标"/>
            <Setter Property="LogarithmBase" Value="10" Note="对数基数"/>
            <Setter Property="IsInterlaced" Value="False" Note="是否启用带状线"/>
            <Setter Property="InterlacedColor" Value="100,100,100" Note="带状线间隔颜色"/>
            <Setter Property="Minimum" Value="" Note="最小值"/>
            <Setter Property="Maximum" Value="" Note="最大值"/>
            <Setter Property="Interval" Value="" Note="间隔值"/>
            <Setter Property="IsLabelAutoFit" Value="True" Note="是否自动调整轴标签"/>
            <Setter Property="IntervalAutoMode" Value="FixedCount" Note="间隔标志[FixedCount:始终在轴上创建固定的间隔数;VariableCount:轴间隔数取决于轴长度]"/>
            <Setter Property="IsMarginVisible" Value="True" Note="是否向轴添加边距"/>
          </Axis.Attribute>
          <Axis.Title Enabled="False" Note="坐标轴文字">
            <Setter Property="Title" Value="坐标轴说明" Note="坐标轴文字内容"/>
            <Setter Property="TitleForeColor" Value="100,100,100" Note="坐标轴文字颜色"/>
            <Setter Property="TextOrientation" Value="Rotated90" Note="文本方向[Horizontal:水平;Rotated90:文本旋转 90 度,成为自上向下的方向;Rotated270:文本旋转 270 度,成为自下向上的方向;Stacked:文本字符不旋转,只是逐个上下排列]"/>
            <TitleFont Note="标题文字样式">
              <Setter Property="Name" Value="黑体" Note="字体"/>
              <Setter Property="Size" Value="16" Note="字体大小"/>
              <Setter Property="Blod" Value="True" Note=" 
                       
                    
                    

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)发布时间:2022-07-10
下一篇:
C#文件操作大全发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap