服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net中资源文件的使用

asp.net中资源文件的使用

2019-09-25 10:38asp.net教程网 ASP.NET教程

.Net是一个丰富的平台,在它的结构中,程序集Assembly是自我描述的安装单元,它可以只包括一个PE(可移植可执行)格式的Dll或exe文件,也可以由多个文件组成,例如资源、Dll和exe等

其中,资源是的范围很广,它可由多种元素组成,包括与用户交互的界面元素(如位图、图标或光标)、应用程序所需数据的自定义文件以及安装 API 使用的版本文件、菜单和对话框等都可以作为资源。为.Net程序集添加资源,就可实现资源重用等功能。使用Visual Studio.Net集成开发环境IDE很容易创建资源文件,把资源添加到工程中的方法和添加窗体、类库一样简单,只是你需要设置资源的“BuildAction”属性为“Embedded Resource”,这样你就可以使用这些资源 
创建资源 
字符串表是极常见的一种资源。要创建这类资源文件,有以下两种方式: 
(1)使用.Net命令行工具ResGen创建。首先创建包含资源内容的文本文件,可使用(记事本、EditPlus等文本编辑器)。该文本文件由所需要的“键值对”组成,键的名称可以在程序中引用,设置键名后把字符串值赋予该键即可完成文件的创建。作为示例,以下语句段产生这样的资源,按下面的格式保存为userinfo.txt文件: 

复制代码代码如下:

Username="Songh"; 
Sex="Boy"; 
Birthday="1973-01-15"; 
Salary="5000RMB"; 


然后,把文本文件转换为资源文件,这仍然通过ResGen工具来实现。执行以下语句:ResGen userinfo.txt,就将生成资源文件userinfo.resources。另外,ResGen还可以创建基于XML格式的.resX资源文件,执行以下命令ResGen userinfo.resources userinfo.resx 就将生成Xml格式的资源userinfo.resx。不过,ResGen工具不支持图象资源的操作,下面的方法就不具有这样的限制。 
(2)使用ResourceWriter类。 为易于创建资源文件,.Net结构提供了ResourceWriter类以支持图象等各种资源类型的创建。ResourceWriter类包含的方法能以系统默认的格式将资源写入输出文件或输出流。与方法1)不同的是,这里统一在一个过程中完成。 
要创建一个资源文件,请调用ResourceWriter类的构造函数初始化类实例并至少提供流名或文件名。资源的实际内容通过调用AddResource方法来完成,AddResource方法将资源指定为名称和值对。资源的实际写入需要调用Generate方法来实现,不过,在调用Close方法关闭该ResourceWriter时将隐式调用Generate方法。 
ResourceWriter.AddResource()方法向要写入资源的列表中添加资源。在创建ResourceWriter类实例后,该方法可以添加至多2GB的资源,下面的重载方法之一用于向资源列表中添加string资源: 

复制代码代码如下:

public void AddResource( 
string name,//键名 
string value//值 
); 


在这里,AddResource方法的第一个参数指定键名称,第二个参数指定值。多次调用该方法就可以完成字符串表的创建。另外,添加图象资源可以通过实例化类Image来实现(这时,请添加System.Drawing名称空间)。 
下面的代码段生成包含字符串表和图象的资源文件userinfo.resources。 

复制代码代码如下:

using System; 
using System.Resources; 
using System.Drawing; 
public class RS 

public static void Main() 

ResourceWriter rw=new 
ResourceWriter("userinfo.resources");//提供文件名以初始化ResourceWriter类实例。 
Image image=Image.FromFile("photo.gif");//实例化Image类 
rw.AddResource("Photo",image);//添加图象 
rw.AddResource("Username","songh");//添加字符串 
rw.AddResource("Sex","boy");//添加字符串 
rw.AddResource("Birthday","1973-01-15");//添加字符串 
rw.AddResource("Salary","5000RMB");//添加字符串 
rw.Close();//关闭ResourceWriter并隐式调用Generate()方法完成资源文件写入磁盘文件。 


上面的代码首先打开图形文件photo.gif,创建一个Image对象。这样做时,这个图形文件必须存在于工程可执行文件的目录(通常是项目的\Bin\Debug目录)下,或者在Image.FromFile()的方法参数中指定图象的完整路径。然后,通过几次调用AddResouce()方法把字符串资源添加到ResourceWriter对象中。最后,调用Close()方法关闭ResourceWriter对象并隐式调用Generate()方法把资源写入文件userinfo.resources。 
编译以上代码并运行就将创建资源文件userinfo.resources。 
以上两种方式生成的资源文件均可以作为一个外部文件添加到程序集中,或者内嵌到Dll或exe中。下面继续说明如何在Windows应用程序使用资源文件。 
使用资源文件 
使用Visual Studio.Net集成开发环境IDE,可以把很容易把资源文件添加到程序集中。只需要在创建的工程中添加已经存在的资源文件,简单设置其属性就可将资源文件嵌入该程序集。下面通过一个C# Windows控制台实例来说明任何使用上面创建的userinfo.resources资源文件。 
首先,创建C# Windows Console项目ResourceUserinfo,打开"项目\添加现有项",找到前面创建的资源文件Userinfo.resources添加到这个工程中; 
然后,选择这个资源文件,将属性BuildAction(生成操作)设置为Embedded Resource(嵌入的资源),这样,资源文件就可以嵌入到输出的程序集中。 
现在,你可以使用这个资源文件了。System.Resources名称空间中的ResourceManager类提供在运行时方便地访问特定资源的途径。具体地可以通过GetObject和GetString方法来实现,以键名为参数就将返回对应的值。 
ResourceManager类的构造函数初始化ResourceManager类的新实例,其重载方法之一查找包含在一些文件中的资源,这些文件是使用给定的 Assembly 从指定根名称导出的。 

复制代码代码如下:

public ResourceManager( 
string baseName, 
Assembly assembly 


其中,参数baseName表示资源的根名称。根名称由应用程序名称空间和资源文件名(不带扩展名)组成。这样,该例中资源的根名称应该是:UserinfoResource.Userinfo,通过调用GetManifestResourceNames()方法也可编程获取该名称。 
另一个参数assembly表示的是当前的主程序集,本例中的主程序集其实也是正在执行的程序集。获取正在执行程序集的一个简单方法是调用Assembly.GetExecutingAssembly()方法。 
在获取ResourceManager实例后,通过指定键名,就可以获得对应的资源。 
下表是程序中使用的部分控件: 
类别 TextBox TextBox TextBox TextBox PictureBox 
名称 username sex birthday salary photo 
这些控件均可直接从工具箱拖放到设计器中。 
完整的源代码为: 
方法一: 

复制代码代码如下:

using System.reflection; 
using System.Resources; 
private System.Resources.ResourceManager rm; 
public Form1() 

InitializeComponent(); 
Assembly assembly=Assembly.GetExecutingAssembly();//获取当前主程序集 
Rm=new ResourceManager("ResourceUserinfo.Userinfo",assembly);//实例化资源管理类 
photo.iamge=(Image)rm.GetObjetct("Photo"); 
username.Text=rm.GetString("Username"); 
sex.Text=rm.GetString("Sex"); 
birthday.Text=rm.GetString("Birthday"); 
salary.Text=rm.GetString("Salary"); 


方法二: 

复制代码代码如下:

Assembly assm = this.GetType().Assembly;//Assembly.LoadFrom(程序集路径); 
foreach (string resName in assm.GetManifestResourceNames()) 

Stream stream = assm.GetManifestResourceStream(resName); 
ResourceReader rr = new ResourceReader(stream); 
IDictionaryEnumerator enumerator = rr.GetEnumerator(); 
while (enumerator.MoveNext()) 

DictionaryEntry de = (DictionaryEntry)enumerator.Current; 
//de.Key是资源名 
//de.Value是资源内容 


运行以上代码,便可取出资源文件内容。 
posted @ 2011-12-15 11:40 Tasting 阅读(21) 评论(0) 编辑 
DoDragDrop 方法的使用 
DoDragDrop方法,用于开始对象的拖放操作。 
在类库中的定义为: 

复制代码代码如下:

[UIPermissionAttribute(SecurityAction.Demand, Clipboard = UIPermissionClipboard.OwnClipboard)] 
public DragDropEffects DoDragDrop( 
Object data, 
DragDropEffects allowedEffects 


  其中data参数为要拖放的数据,如果拖动操作需要于另一个进程的应用程序相互操作,data代表的数据应该是基本托管类(String,BitMap,或MetaFile),或者是实现 ISerializable 或IDataObject的对象。 allowedEffects参数表示拖放的效果,为一个枚举值(DragDropEffects).返回值也为DragDropEffects枚举值。 
  当开始调用DoDragDrop方法拖动一个数据对象时,DoDragDrops在拖放过程中,检测当前光标位置下的控件是不是有效的放置目标。如果当前光标下的控件是有效的放置目标,则GiveFeedBack事件以指定的拖放效果引发。在检测当前位置光标是否为有效的拖放目标时,DoDragDrops方法同时跟踪光标位置,键盘状态和鼠标状态的更改。 
   (1)如果用于移出了一个窗口,则引发DragLeave事件。 
  (2)如果移入了另外一个控件,则引发该控件的DragEnter事件。 
  (3)如果鼠标移动,但是停留在一个控件中,则引发DragOver事件。 
如果检测到更改了键盘或者鼠标状态,则引发拖放源的QueryContinueDrag事件, 并根据事件的QueryContinueDragEventArgs的Action属性值确定继续拖动,放置数据或取消操作。 
(1)如果Action属性指定为Continue,则将引发DragOver事件。 
(2)如果Action属性指定为Drop,则将放置效果返回给源,以便应用程序对数据进行适当的操作;例如,如果是移动操作,则剪切数据。 
(3)如果是DragAction的值为Cancel,则引发DragLeave事件 
从csdn上摘抄一段示例代码: 
  下面的代码示例演示在两个 ListBox 控件之间的拖放操作。当拖动动作启动时,该示例调用 DoDragDrop 方法。在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation..::.DragSize,则启动拖动动作。IndexFromPoint 方法用于确定在 MouseDown 事件期间要拖动的项的索引。 
  该示例还演示如何对拖放操作使用自定义光标。该示例要求应用程序目录中存在两个光标文件:3dwarro.cur 和 3dwno.cur,分别用于自定义拖动光标和禁止停放光标。如果选中 UseCustomCursorsCheckCheckBox,则使用自定义光标。自定义光标在 GiveFeedback 事件处理程序中设置。 
  键盘状态在右 ListBox 的 DragOver 事件处理程序中计算,以确定基于 Shift、Ctrl、Alt 或 Ctrl+Alt 键的状态将发生哪种拖动操作。放置动作在 ListBox 中发生的位置也在 DragOver 事件期间确定。如果要放置的数据不是 String,则 DragDropEffects 中将把 DragEventArgs.sEffect 设置为 None。最后,停放状态在 DropLocationLabelLabel 中显示。 
  要放置的用于右 ListBox 的数据在 DragDrop 事件处理程序中确定,并且在 ListBox 中的适当位置添加该 String 值。如果拖动操作移动到窗体边框的外面,则 QueryContinueDrag 事件处理程序中将取消拖放操作 

复制代码代码如下:

using System; 
using System.Drawing; 
using System.Windows.Forms; 
namespace Snip_DragNDrop 

public class Form1 : System.Windows.Forms.Form 

private System.Windows.Forms.ListBox ListDragSource; 
private System.Windows.Forms.ListBox ListDragTarget; 
private System.Windows.Forms.CheckBox UseCustomCursorsCheck; 
private System.Windows.Forms.Label DropLocationLabel; 
private int indexOfItemUnderMouseToDrag; 
private int indexOfItemUnderMouseToDrop; 
private Rectangle dragBoxFromMouseDown; 
private Point screenOffset; 
private Cursor MyNoDropCursor; 
private Cursor MyNormalCursor; 
/// The main entry point for the application. 
[STAThread] 
static void Main() 

Application.Run(new Form1()); 

public Form1() 

this.ListDragSource = new System.Windows.Forms.ListBox(); 
this.ListDragTarget = new System.Windows.Forms.ListBox(); 
this.UseCustomCursorsCheck = new System.Windows.Forms.CheckBox(); 
this.DropLocationLabel = new System.Windows.Forms.Label(); 
this.SuspendLayout(); 
// ListDragSource 
this.ListDragSource.Items.AddRange(new object[] {"one", "two", "three", "four", 
"five", "six", "seven", "eight", 
"nine", "ten"}); 
this.ListDragSource.Location = new System.Drawing.Point(10, 17); 
this.ListDragSource.Size = new System.Drawing.Size(120, 225); 
this.ListDragSource.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseDown); 
this.ListDragSource.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.ListDragSource_QueryContinueDrag); 
this.ListDragSource.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseUp); 
this.ListDragSource.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ListDragSource_MouseMove); 
this.ListDragSource.GiveFeedback += new System.Windows.Forms.GiveFeedbackEventHandler(this.ListDragSource_GiveFeedback); 
// ListDragTarget 
this.ListDragTarget.AllowDrop = true; 
this.ListDragTarget.Location = new System.Drawing.Point(154, 17); 
this.ListDragTarget.Size = new System.Drawing.Size(120, 225); 
this.ListDragTarget.DragOver += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragOver); 
this.ListDragTarget.DragDrop += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragDrop); 
this.ListDragTarget.DragEnter += new System.Windows.Forms.DragEventHandler(this.ListDragTarget_DragEnter); 
this.ListDragTarget.DragLeave += new System.EventHandler(this.ListDragTarget_DragLeave); 
// UseCustomCursorsCheck 
this.UseCustomCursorsCheck.Location = new System.Drawing.Point(10, 243); 
this.UseCustomCursorsCheck.Size = new System.Drawing.Size(137, 24); 
this.UseCustomCursorsCheck.Text = "Use Custom Cursors"; 
// DropLocationLabel 
this.DropLocationLabel.Location = new System.Drawing.Point(154, 245); 
this.DropLocationLabel.Size = new System.Drawing.Size(137, 24); 
this.DropLocationLabel.Text = "None"; 
// Form1 
this.ClientSize = new System.Drawing.Size(292, 270); 
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.ListDragSource, 
this.ListDragTarget, this.UseCustomCursorsCheck, 
this.DropLocationLabel}); 
this.Text = "drag-and-drop Example"; 
this.ResumeLayout(false); 

private void ListDragSource_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 

// Get the index of the item the mouse is below. 
indexOfItemUnderMouseToDrag = ListDragSource.IndexFromPoint(e.X, e.Y); 
if (indexOfItemUnderMouseToDrag != ListBox.NoMatches) { 
// Remember the point where the mouse down occurred. The DragSize indicates 
// the size that the mouse can move before a drag event should be started. 
Size dragSize = SystemInformation.DragSize; 
// Create a rectangle using the DragSize, with the mouse position being 
// at the center of the rectangle. 
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width /2), 
e.Y - (dragSize.Height /2)), dragSize); 
} else 
// Reset the rectangle if the mouse is not over an item in the ListBox. 
dragBoxFromMouseDown = Rectangle.Empty; 

private void ListDragSource_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { 
// Reset the drag rectangle when the mouse button is raised. 
dragBoxFromMouseDown = Rectangle.Empty; 

private void ListDragSource_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 

if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { 
// If the mouse moves outside the rectangle, start the drag. 
if (dragBoxFromMouseDown != Rectangle.Empty && 
!dragBoxFromMouseDown.Contains(e.X, e.Y)) { 
// Create custom cursors for the drag-and-drop operation. 
try { 
MyNormalCursor = new Cursor("3dwarro.cur"); 
MyNoDropCursor = new Cursor("3dwno.cur"); 
} catch { 
// An error occurred while attempting to load the cursors, so use 
// standard cursors. 
UseCustomCursorsCheck.Checked = false; 
}finally { 
// The screenOffset is used to account for any desktop bands 
// that may be at the top or left side of the screen when 
// determining when to cancel the drag drop operation. 
screenOffset = SystemInformation.WorkingArea.Location; 
// Proceed with the drag-and-drop, passing in the list item. 
DragDropEffects dropEffect = ListDragSource.DoDragDrop(ListDragSource.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link); 
// If the drag operation was a move then remove the item. 
if (dropEffect == DragDropEffects.Move) { 
ListDragSource.Items.RemoveAt(indexOfItemUnderMouseToDrag); 
// Selects the previous item in the list as long as the list has an item. 
if (indexOfItemUnderMouseToDrag > 0) 
ListDragSource.SelectedIndex = indexOfItemUnderMouseToDrag -1; 
else if (ListDragSource.Items.Count > 0) 
// Selects the first item. 
ListDragSource.SelectedIndex =0; 

// Dispose of the cursors since they are no longer needed. 
if (MyNormalCursor != null) 
MyNormalCursor.Dispose(); 
if (MyNoDropCursor != null) 
MyNoDropCursor.Dispose(); 




private void ListDragSource_GiveFeedback(object sender, System.Windows.Forms.GiveFeedbackEventArgs e) 

// Use custom cursors if the check box is checked. 
if (UseCustomCursorsCheck.Checked) { 
// Sets the custom cursor based upon the effect. 
e.UseDefaultCursors = false; 
if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move) 
Cursor.Current = MyNormalCursor; 
else 
Cursor.Current = MyNoDropCursor; 


private void ListDragTarget_DragOver(object sender, System.Windows.Forms.DragEventArgs e) 

// Determine whether string data exists in the drop data. If not, then 
// the drop effect reflects that the drop cannot occur. 
if (!e.Data.GetDataPresent(typeof(System.String))) { 
e.Effect = DragDropEffects.None; 
DropLocationLabel.Text = "None - no string data."; 
return; 

// Set the effect based upon the KeyState. 
if ((e.KeyState & (8+32)) == (8+32) && 
(e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) { 
// KeyState 8 + 32 = CTL + ALT 
// Link drag-and-drop effect. 
e.Effect = DragDropEffects.Link; 
} else if ((e.KeyState & 32) == 32 && 
(e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) { 
// ALT KeyState for link. 
e.Effect = DragDropEffects.Link; 
} else if ((e.KeyState & 4) == 4 && 
(e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { 
// SHIFT KeyState for move. 
e.Effect = DragDropEffects.Move; 
} else if ((e.KeyState & 8) == 8 && 
(e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) { 
// CTL KeyState for copy. 
e.Effect = DragDropEffects.Copy; 
} else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) { 
// By default, the drop action should be move, if allowed. 
e.Effect = DragDropEffects.Move; 
} else 
e.Effect = DragDropEffects.None; 
// Get the index of the item the mouse is below. 
// The mouse locations are relative to the screen, so they must be 
// converted to client coordinates. 
indexOfItemUnderMouseToDrop = 
ListDragTarget.IndexFromPoint(ListDragTarget.PointToClient(new Point(e.X, e.Y))); 
// Updates the label text. 
if (indexOfItemUnderMouseToDrop != ListBox.NoMatches){ 
DropLocationLabel.Text = "Drops before item #" + (indexOfItemUnderMouseToDrop + 1); 
} else 
DropLocationLabel.Text = "Drops at the end."; 

private void ListDragTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 

// Ensure that the list item index is contained in the data. 
if (e.Data.GetDataPresent(typeof(System.String))) { 
Object item = (object)e.Data.GetData(typeof(System.String)); 
// Perform drag-and-drop, depending upon the effect. 
if (e.Effect == DragDropEffects.Copy || 
e.Effect == DragDropEffects.Move) { 
// Insert the item. 
if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) 
ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item); 
else 
ListDragTarget.Items.Add(item); 


// Reset the label text. 
DropLocationLabel.Text = "None"; 

private void ListDragSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e) { 
// Cancel the drag if the mouse moves off the form. 
ListBox lb = sender as ListBox; 
if (lb != null) { 
Form f = lb.FindForm(); 
// Cancel the drag if the mouse moves off the form. The screenOffset 
// takes into account any desktop bands that may be at the top or left 
// side of the screen. 
if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) || 
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) || 
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) || 
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) { 
e.Action = DragAction.Cancel; 



private void ListDragTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { 
// Reset the label text. 
DropLocationLabel.Text = "None"; 

private void ListDragTarget_DragLeave(object sender, System.EventArgs e) { 
// Reset the label text. 
DropLocationLabel.Text = "None"; 



对用这种拖放操作和微软的服务,容器模式的关系,留在以后再学习。 
posted @ 2011-12-15 11:16 Tasting 阅读(92) 评论(0) 编辑 
关于接口的使用 
概述: 接口的使用体现了一种泛化的思想。 
应用场景之一是: 
(1)多个类都要实现某些动作,而该动作具体实现的方式又不一样。 
如对于创建条件来的窗体来说,要实现添加整形参数,bool类型参数和字符串类型参数的条件添加的窗体。而在具体的实现整形和字符串的窗体,以及bool类型的添加的窗体过程中,实现细节又不一样。 

复制代码代码如下:

public interface IExpressionForm 

ConditionItemEntity CIEntity { get; set; } 
ConditionBranchEntity CIEntity { get; set; } 
event Handle OnExpressionHandled; } 


  而对于每个窗体来说,都需要包含CIEntity,CIEntity属性和OnExpressionHandled添加条件后的事件,因此在IExpressionForm 接口中定义了CIEntity,CIEntity,和OnExpressionHandled。 
接着实现整形参数的条件添加窗体 

复制代码代码如下:

public partial class frmNumericCondition : Form, IExpressionForm 

public frmNumericCondition() 

InitializeComponent(); 

public ConditionItemEntity CIEntity { get; set; } 
public ConditionBranchEntity CBEntity { get; set; } 
public event Handle OnExpressionHandled; 


  然后是字符型参数条件添加窗体 

复制代码代码如下:

public partial class frmVarcharCondition : Form, IExpressionForm 

public frmVarcharCondition() 

InitializeComponent(); 

public ConditionItemEntity CIEntity { get; set; } 
public ConditionBranchEntity CBEntity { get; set; } 
public event Handle OnExpressionHandled; 


以此类推,实现其它参数类型条件添加的窗体。 
那我这样实现的目的的好处是什么呢?接下来我们来看看,我定义的一个产生窗体的函数 

复制代码代码如下:

public static IExpressionForm CreateExpressionForm(ConditionType ct) 

IExpressionForm frm = null; 
if (ct == ConditionType.bit) 
frm = new frmBitCondition(); 
else if (ct == ConditionType.datetime) 
frm = new frmDateTimeCondition(); 
else if (ct == ConditionType.numeric) 
frm = new frmNumericCondition(); 
else if (ct == ConditionType.varchar) 
frm = new frmVarcharCondition(); 
return frm; 


从定义中我们可以看出,返回值类型为IExpressionForm ,是我在上边定义的接口。因此该函数可以返回一切实现了IExpressionForm 接口的类。如frmVarcharCondition 和frmNumericCondition. 
这样就简单的实现了工厂模式,程序可以用很好的扩展性。 
以上只是接口的应用场景之一,也是自己在写代码的时候发现的。写的不好。但也要写,一方面是要总结工作和学习,在总结的时候可以思考和发现,也希望能给阅读文章的人一些帮助。

 

延伸 · 阅读

精彩推荐