fastCSharp代码生成器发布
由于两个ACM题花了差不多两个星期的时间,拖延了代码生成器的发布(下载)。
不过通过这段时间的折腾,对于C#细节优化有了更多的了解。知道了结构体数组元素赋值写个Set函数比=new要好,知道了静态字段的访问和引用成员字段访问一样低效(相对于堆栈访问),知道了临时变量的数量必须要自己控制(C#编译器的寄存器优化并不智能),知道了小函数内联是靠不住的,更加的重视非安全数组元素的访问次数了...
还是回到正题,上个月准备讨论一下代码生成器重新实现的问题,没有什么反馈,所以还是按照我以前的方案重写代码。我简单的说一下方案实现的原理与过程。
1、在需要代码生成功能的项目属性的后期生成事件中执行命令调用带参的fastCSharp.exe,代码生成器是基于.net元数据的,所以需要程序集相关信息:
if exist D:\fastCSharp\bin\Release\fastCSharp.exe D:\fastCSharp\bin\Release\fastCSharp.exe $(ProjectName) $(ProjectDir) $(TargetPath) $(TargetName)
list<keyValue<Type, setup.auto>> autos = setup.ui.CurrentAssembly.GetTypes()
.getFind(type => !type.IsInterface && !type.IsAbstract && type.isInterface(typeof(setup.IAuto)))
.getArray(type => new keyValue<Type, setup.auto>(type, type.customAttribute<setup.auto>(false)))
.getFind(value => value.Value != null && value.Value.IsSetup && value.Value.IsAuto);
setup.ui.Setup(autos, parameter, false);
string[] codes = parameter.Types.getArray(type => new definition { Type = type, Auto = type.customAttribute<auto>(false) })
.getFind(type => type.Auto != null && type.Auto.IsSetup && type.Auto.DependType == typeof(cSharper))
.getArray(type => type.ToString());keyValue<Type, ajax>[] ajaxs = parameter.Types
.getArray(type => new keyValue<Type, ajax>(type, type.customAttribute<ajax>(false)))
.getFindArray(ajax => ajax.Value != null && ajax.Value.IsSetup);
foreach (keyValue<Type, ajax> ajax in ajaxs)
{
type = ajax.Key;
Attribute = ajax.Value;
UnknownToJsonType = ajax.Value.UnknownToJson != null && ajax.Value.UnknownToJson.isInterface(typeof(IUnknownToJson)) ? (memberType)ajax.Value.UnknownToJson : null;
UnknownParseJsonType = ajax.Value.UnknownParseJson != null && ajax.Value.UnknownParseJson.isInterface(typeof(IUnknownParseJson)) ? (memberType)ajax.Value.UnknownParseJson : null;
Members = setup.memberInfo.GetMembers<ajax>(ajax.Key, setup.memberInfo.filter.PublicInstance, false, true);
if (Attribute.IsToJson) memberMap.create(ajax.Key);
if (Attribute.IsParseJson && !ajax.Key.isNull()) copy.create(ajax.Key);
create(true);
}