.NET Framework基础知识(五)(转载)
1、程序集:是 .NET Framework 应用程序的构造块;程序集构成了部署、版本控制、重复使用、激活范围控制和
安全权限的基本单元。
2、程序集的优点:版本控制问题、最终解决DLL冲突
3、程序集分为两种:强命名程序集和弱命名程序集。
4、.NET Framework 为开发全球通用的应用程序提供了广泛的支持。在开发全球通用的应用程序时,
建议将此过程分为三个步骤:全球化、本地化分析和本地化。
(1)全球化是全球通用应用程序创建过程的第一步。在这一步中将编写应用程序的可执行代码。
一个真正的全球化应用程序应是非特定区域性和非特定语言的。因此,应集中精力创建能够支持适用于所有用户的
本地化用户界面和区域数据的应用程序。注意,尽管全球化应用程序具有这种灵活性,但全球化过程本身并不涉及用户界面的翻译。
相反,应致力于使创建的应用程序具有对来自应用程序所支持的所有区域性和地区的用户均有效的功能。
(2)在继续进行本地化之前,应执行中间检查以确定应用程序的 本地化分析。如果应用程序可本地化,
说明已正确地将应用程序的可执行代码同其资源分开。如果正确地评估了应用程序的可本地化性,在本地化期间就
不需要修改应用程序的源代码。
(3)生成全球通用的应用程序的最后一步是 本地化,在这一步中,需要针对特定的区域性或地区自定义应用程序。
如果已正确执行了全球化和本地化分析这两个步骤,则本地化主要包括用户界面的翻译。
5、设计和开发全球通用的应用程序有以下几个优点:
(1)全球范围的收入。
(2)可以迅速添加对新区域性的支持。
(3)使用资源的效率更高。
6、System.Globalization 命名空间包含定义区域性相关信息的类,这些信息包括语言、国家/地区、
使用的日历、日期、货币和数字的格式模式以及字符串的排序顺序。我们可以使用这些类编写全球化(国际化)应用程序。
7、并行编程:许多个人计算机和工作站都有两个或四个内核(即 CPU),使多个线程能够同时执行。
在不久的将来,计算机预期会有更多的内核。 为了利用当今和未来的硬件,您可以对代码进行并行化,以将工作分摊在多个处理器上。
8、特性、反射、泛型综合实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using System.Security.Permissions;
using System.Reflection;
namespace RefAtrr
{
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property,
AllowMultiple = true, Inherited = true)] //Author属性只能用于类和结构,AllowMultiple是否允许多次用属性,
Inherited是这个属性是滞延续到子类。
public class SqlTableAttribute : System.Attribute
{
private string name;
public SqlTableAttribute(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property,
AllowMultiple = true, Inherited = true)] //Author属性只能用于类和结构,AllowMultiple是否允许多次用属性,
Inherited是这个属性是滞延续到子类。
public class SqlFieldAttribute : System.Attribute
{
private string name;
Type fieldtype;
bool key;
public SqlFieldAttribute(string name, Type fieldtype, bool key)
{
this.name = name;
this.fieldtype = fieldtype;
this.key = key;
}
public Type FieldType
{
get { return fieldtype; }
set { fieldtype = value; }
}
public bool Key
{
get { return key; }
set { key = value; }
}
public string Name
{
get { return name; }
}
}
interface IEntityTable
{
}
[SqlTable("StuUsers")]
class User : IEntityTable
{
[SqlField("ID", typeof(int), true)]
public int Number
{
get;
set;
}
[SqlField("StuNum", typeof(string), false)]
public string StuNumber
{
get;
set;
}
[SqlField("Passwd", typeof(string), false)]
public string Password
{
get;
set;
}
}
[SqlTable("Announcement")]
class AnnClass : IEntityTable
{
[SqlField("ID", typeof(int), true)]
public int Number
{
get;
set;
}
[SqlField("PubTitle", typeof(string), false)]
public string Title
{
get;
set;
}
[SqlField("PubCon", typeof(string), false)]
public string Conn
{
get;
set;
}
}
enum SqlType
{
Select,
Update,
Insert,
Delete
}
class Program
{
static void Main()
{
AnnClass ac = new AnnClass();
ac.Number = 12;
ac.Title = "通知";
ac.Conn = "好好工作,天天上网";
User user = new User();
user.Number = 1;
user.StuNumber = "101000242";
user.Password = "111111";
//用接口实现
EntitHandle Eh = new EntitHandle();
Console.WriteLine(Eh.GreatSQL(user, SqlType.Insert));
Console.WriteLine("--------------------------------------------------");
Console.WriteLine(Eh.GreatSQL(ac, SqlType.Delete));
Console.WriteLine("--------------------------------------------------");
//用泛型实现
EntityHandle<IEntityTable> EhG = new EntityHandle<IEntityTable>();
Console.WriteLine(EhG.GreatSQL(ac, SqlType.Select ));
Console.WriteLine("--------------------------------------------------");
Console.WriteLine(EhG.GreatSQL(user, SqlType.Update ));
Console.WriteLine("--------------------------------------------------");
}
}
class EntitHandle
{
public string GreatSQL(IEntityTable EntityTable, SqlType sqltype)
{
Type usertype = EntityTable.GetType();
object[] ObjArr = usertype.GetCustomAttributes(true);
string tablename = ((SqlTableAttribute)ObjArr[0]).Name;
PropertyInfo[] properties = usertype.GetProperties();
string SQL = "";
switch (sqltype)
{
case SqlType.Select:
SQL = Select(EntityTable, tablename, properties);
break;
case SqlType.Delete:
SQL = Delete(EntityTable, tablename, properties);
break;
case SqlType.Insert:
SQL = Insert(EntityTable, tablename, properties);
break;
case SqlType.Update:
SQL = Update(EntityTable, tablename, properties);
break;
}
return SQL;
}
string Insert(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
foreach (PropertyInfo o in properties)
{
value += ((SqlFieldAttribute)o.GetCustomAttributes(true)[0]).Name + "='"
+ o.GetValue(EntityTable, null).ToString() + "',";
}
value = value.TrimEnd(',');
string SQL = string.Format("insert into {0} values({1})", tablename, value);
return SQL;
}
string Update(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
string condition = "";
foreach (PropertyInfo o in properties)
{
SqlFieldAttribute sfa = (SqlFieldAttribute)o.GetCustomAttributes(false)[0];
value += sfa.Name + "='" + o.GetValue(EntityTable, null).ToString() + "',";
if (sfa.Key)
{
string keyname = sfa.Name;
string keyvalue = o.GetValue(EntityTable, null).ToString();
condition = string.Format(" where {0}='{1}'", keyname, keyvalue);
}
}
value = value.TrimEnd(',') + condition;
string SQL = string.Format("update {0} set {1} ", tablename, value);
return SQL;
}
string Delete(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string condition = "";
foreach (PropertyInfo o in properties)
{
SqlFieldAttribute sfa = (SqlFieldAttribute)o.GetCustomAttributes(false)[0];
if (sfa.Key)
{
string keyname = sfa.Name;
string keyvalue = o.GetValue(EntityTable, null).ToString();
condition = string.Format(" where {0}='{1}'", keyname, keyvalue);
}
}
string SQL = string.Format("delete {0} where {1}", tablename, condition);
return SQL;
}
string Select(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
foreach (PropertyInfo o in properties)
{
value += ((SqlFieldAttribute)o.GetCustomAttributes(true)[0]).Name + ",";
}
value = value.TrimEnd(',');
string SQL = string.Format("select {0} from {1}", value, tablename);
return SQL;
}
}
class EntityHandle<T> where T : IEntityTable
{
public string GreatSQL(T EntityTable, SqlType sqltype)
{
Type usertype = EntityTable.GetType();
object[] ObjArr = usertype.GetCustomAttributes(true);
string tablename = ((SqlTableAttribute)ObjArr[0]).Name;
PropertyInfo[] properties = usertype.GetProperties();
string SQL = "";
switch (sqltype)
{
case SqlType.Select:
SQL = Select(EntityTable, tablename, properties);
break;
case SqlType.Delete:
SQL = Delete(EntityTable, tablename, properties);
break;
case SqlType.Insert:
SQL = Insert(EntityTable, tablename, properties);
break;
case SqlType.Update:
SQL = Update(EntityTable, tablename, properties);
break;
}
return SQL;
}
string Insert(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
foreach (PropertyInfo o in properties)
{
value += ((SqlFieldAttribute)o.GetCustomAttributes(true)[0]).Name + "='"
+ o.GetValue(EntityTable, null).ToString() + "',";
}
value = value.TrimEnd(',');
string SQL = string.Format("insert into {0} values({1})", tablename, value);
return SQL;
}
string Update(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
string condition = "";
foreach (PropertyInfo o in properties)
{
SqlFieldAttribute sfa = (SqlFieldAttribute)o.GetCustomAttributes(false)[0];
value += sfa.Name + "='" + o.GetValue(EntityTable, null).ToString() + "',";
if (sfa.Key)
{
string keyname = sfa.Name;
string keyvalue = o.GetValue(EntityTable, null).ToString();
condition = string.Format(" where {0}='{1}'", keyname, keyvalue);
}
}
value = value.TrimEnd(',') + condition;
string SQL = string.Format("update {0} set {1} ", tablename, value);
return SQL;
}
string Delete(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string condition = "";
foreach (PropertyInfo o in properties)
{
SqlFieldAttribute sfa = (SqlFieldAttribute)o.GetCustomAttributes(false)[0];
if (sfa.Key)
{
string keyname = sfa.Name;
string keyvalue = o.GetValue(EntityTable, null).ToString();
condition = string.Format(" where {0}='{1}'", keyname, keyvalue);
}
}
string SQL = string.Format("delete {0} where {1}", tablename, condition);
return SQL;
}
string Select(IEntityTable EntityTable, string tablename, PropertyInfo[] properties)
{
string value = "";
foreach (PropertyInfo o in properties)
{
value += ((SqlFieldAttribute)o.GetCustomAttributes(true)[0]).Name + ",";
}
value = value.TrimEnd(',');
string SQL = string.Format("select {0} from {1}", value, tablename);
return SQL;
}
}
}
本文出自 “大懒丫头” 博客,请务必保留此出处http://lanyatou.blog.51cto.com/3306130/633163
出处:http://www.cnblogs.com/babycool/
本文首发博客园,版权归作者跟博客园共有。
转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。