当前位置: 首页 > 编程语言 > C#

如何通过t4生成自定义类?如何分享

时间:2023-04-10 14:50:11 C#

通过t4生成自定义类?我正在尝试通过T4生成以下代码。使用系统;使用MyDAL;namspaceTest{//选择...(intid){//做某事...}}//删除、更新、保存publicpartialclassMyCustomer{publicboolSave(newMyCustomer){//做某事...}publicbooldelete(intid){//做点什么...}publicboolUpdate(intid){//做点什么...}}}到目前为止我的T4模板:namespaceDbClasses{}我不确定你面对的是什么问题:但是你的T4码没有问题。第二个类中Save方法的签名是新的,这是一个错误:publicboolSave(newMyCustomer)应该是publicboolSave(MyCustomercustomer)另外,GetById应该返回一个元素而不是一个列表。我通过为Table、Column等编写虚拟类来尝试在没有连接到数据库的情况下编写的代码,并且您编写的T4代码没有任何问题,只是您编写的代码没有按照您的要求,而不是创建属性。它只是为每个具有公共字段(不是属性)的表创建一个非部分类。如果你想生成部分类和属性(和方法),你的方向是正确的(我添加了制表符和换行符以使其在生成的文件中看起来不错,你可以按Ctrl+K,Ctrl+D在那里重新格式化它though:GetById(intid)nt{ntt//dosomethingnt}");//endclassWriteLine("}");//输出注释并开始classWriteLine("//Delete,Update,Save");WriteLine("publicpartialclassMy"+tablo.Name+"n{");WriteLine("tpublicboolSave(My"+tablo.Name+""+tablo.Name+")nt{ntt//dosomethingnt}");WriteLine("tpublicboolDelete(intid)nt{ntt//dosomethingnt}");WriteLine("tpublicboolUpdate(intid)nt{ntt//dosomethingnt}");WriteLine("}");}#>这还不完整,但也许下面的T4可以作为一些灵感。这是如何使用它:);varcustomers=sqlCommand.Get_Customer(Id:10,CompId:20).ToArray();foreach(varcustomerincustomers){Console.WriteLine(customer.Id);Console.WriteLine(customer.CompId);Console.WriteLine(customer.FirstName);Console.WriteLine(customer.LastName);Console.WriteLine(customer.OrgNo);}vardeleteCount=sqlCommand.Delete_Customer(Id:20,CompId:20);Console.WriteLine("删除行数:{0}",deleteCount);Console.ReadKey();}T4:以上是C#学习教程:如何通过t4生成自定义类?所有分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——//ReSharperdisableInconsistentNaming//ReSharperdisablePartialTypeWithSinglePartnamespace{usingSystem;使用System.Collections.Generic;使用系统.Data.SqlClient;静态部分类扩展s{staticintLookup(Dictionarydic,stringkey){intvalue;返回dic.TryGetValue(键,输出值)?值:-1;}staticbyte[]GetByteArray(thisSqlDataReaderreader,intindex){varbyteLength=reader.GetBytes(index,0,null,0,0);varbytes=newbyte[byteLength];reader.GetBytes(index,0,bytes,0,bytes.Length);返回字节;}//----------------------------------------------------------------//删除//----------------------------------------------------------------publicstaticintDelete_(thisSqlCommand命令().Where(c=>c.InPrimaryKey)){#>,){if(command!=null){command.CommandText="DELETEFROM.";命令.Parameters.Clear();().Where(c=>c.InPrimaryKey)){#>command.Parameters.AddWithValue(@"",);返回命令.ExecuteNonQuery();}否则{返回0;}}//----------------------------------------------------------------//------------------------------------------------------------------//获取//----------------------------------------------------------------publicstaticIEnumerable>Get_(这个SqlCommand命令().Where(c=>c.InPrimaryKey)){#>,){if(command!=null){command.CommandText="SELECT*FROM.";命令.Parameters.Clear();().Where(c=>c.InPrimaryKey)){#>command.Parameters.AddWithValue(@"",);使用(varreader=command.ExecuteReader()){foreach(varrowinreader.To_()){yieldreturnrow;}}}}//----------------------------------------------------------------//----------------------------------------------------------------//反序列化一个//----------------------------------------------------------------publicstaticIEnumerable>To_(thisSqlDataReaderreader){if(reader!=null){varfieldCount=reader.FieldCount;vardictionary=newDictionary();for(variter=0;iter();()){#>{varindex=Lookup(dictionary,"");如果(索引>-1){实例。=读者。(指数);}}收益返回实例;}}}//----------------------------------------------------------------}//--------------------------------------------------------------------//桌子://-----------------------------------------------partialclass{()){#>//(,,)public{get;set;}}//--------------------------------------------------------------------}s_typeDescriptors=newDictionary(){//不支持://----------//None,//UserDefinedDataType,//UserDefinedDataType,//XML,//SysName,//UserDefinedTableType,//HierarchyId,//Geometry,//Geography{SqlDataType.BigInt,newDataTypeDescriptor(true,"Int64")},{SqlDataType.Binary,newDataTypeDescriptor(true,"Byte")},{SqlDataType.Bit,newDataTypeDescriptor(true,"Boolean")},{SqlDataType.Char,newDataTypeDescriptor(false,"String")},{SqlDataType.DateTime,新DataTypeDescriptor(true,"DateTime")},{SqlDataType.Decimal,newDataTypeDescriptor(true,"Decimal")},{SqlDataType.Float,newDataTypeDescriptor(true,"Double")},{SqlDataType.Image,newDataTypeDescriptor(false,"byte[]","GetByteArray")},{SqlDataType.Int,newDataTypeDescriptor(true,"Int32")},{SqlDataType.Money,newDataTypeDescriptor(true,"Decimal")},{SqlDataType.NChar,newDataTypeDescriptor(false,"String")},{SqlDataType.NText,newDataTypeDescriptor(false,"String")},{SqlDataType.NVarChar,newDataTypeDescriptor(false,"String")},{SqlDataType.NVarCharMax,newDataTypeDescriptor(false,"String")},{SqlDataType.Real,newDataTypeDescriptor(true,"Double")},{SqlDataType.SmallDateTime,newDataTypeDescriptor(true,"DateTime")},{SqlDataType.SmallInt,newDataTypeDescriptor(true,"Int16")},{SqlDataType.SmallMoney,newDataTypeDescriptor(true,"Decimal")},{SqlDataType.Text,newDataTypeDescriptor(false,"String")},{SqlDataType.Timestamp,newDataTypeDescriptor(false,"byte[]","GetByteArray")},{SqlDataType.TinyInt,newDataTypeDescriptor(true,"Byte")},{SqlDataType.UniqueIdentifier,newDataTypeDescriptor(false,"byte[]","GetByteArray")},{SqlDataType.VarBinary,newDataTypeDescriptor(false,"byte[]","GetByteArray")},{SqlDataType.VarBinaryMax,newDataTypeDescriptor(false,"byte[]","GetByteArray")},{SqlDataType.VarChar,newDataTypeDescriptor(false,"String")},{SqlDataType.VarCharMax,newDataTypeDescriptor(false,"String")},{SqlDataType.Variant,newDataTypeDescriptor(false,"object","GetValue")},{SqlDataType.Numeric,newDataTypeDescriptor(true,"Decimal")},{SqlDataType.Date,newDataTypeDescriptor(true,"DateTime")},{SqlDataType.Time,newDataTypeDescriptor(true,"TimeSpan")},{SqlDataType.DateTimeOffset,newDataTypeDescriptor(true,"DateTimeOffset")},{SqlDataType.DateTime2,newDataTypeDescriptor(true,"DateTime")},};静态字符串GetGetter(列列){vardataType=column.DataType;DataTypeDescriptor描述符;if(!s_typeDescriptors.TryGetValue(dataType.SqlDataType,outdescriptor)){return"Error__Unsupported_Type_"+dataType.SqlDataType;}返回描述符.Getter;}staticstringGetType(Column列){vardataType=column.DataType;DataTypeDescriptor描述符;if(!s_typeDescriptors.TryGetValue(dataType.SqlDataType,outdescriptor)){return"Error__Unsupported_Type_"+dataType.SqlDataType;}if(column.Nullable&&descriptor.IsValueType){returndescriptor.Type+"?";}返回描述符。类型;}staticstringGetWhereClause(Tabletable){varsb=newStringBuilder("WHERE");变种首先=真;foreach(varcolumnintable.Columns.OfType().Where(c=>c.InPrimaryKey)){if(first){first=false;}else{sb.Append("AND");}sb.AppendFormat("{0}=@{0}",column.Name);}returnsb.ToString();}#>本文整理自网络,不代表立场,如涉及侵权,请右击联系管理员删除,如有转载请注明出处: