//not complete yet! public static NbRefFieldValue GetRefFieldValue(Expression <Func <object, object> > action, Type classType) { var expression = GetMemberInfo(action); var fieldInfo = classType.GetField(expression.Member.Name, BindingFlags.Public | BindingFlags.Static); if (fieldInfo == null) { throw new NotSupportedException("不支持非FieldInfo的计算!"); } var key = string.Format("{0}.{1}", classType.Name, expression.Member.Name); if (!nbRefFieldValueCache.ContainsKey(key)) { var customAttributes = fieldInfo.GetCustomAttributes(typeof(NbRefFieldAttribute), false); if (customAttributes.Length > 0) { var att = (NbRefFieldAttribute)customAttributes[0]; var fieldValue = new NbRefFieldValue { Description = att.Description, FieldName = fieldInfo.Name, ValueBag = att.ValueBag }; var value = fieldInfo.GetValue(fieldInfo); fieldValue.FieldValue = value.ToString(); nbRefFieldValueCache.Add(key, fieldValue); //Console.WriteLine("cached!"); } } return(nbRefFieldValueCache[key]); }
private static void ShouldOK(string code, string name, string desc, NbRefFieldValue value) { value.FieldValue.ShouldEqual(code); value.FieldName.ShouldEqual(name); value.Description.ShouldEqual(desc); AssertHelper.WriteLine("----"); }
private static void AppendStaticField(Type classType, StringBuilder sb) { var fieldInfos = classType.GetFields(); bool appendTypeInfo = false; foreach (var fieldInfo in fieldInfos) { var customAttributes = fieldInfo.GetCustomAttributes(typeof(NbRefFieldAttribute), false); bool needAppendLine = customAttributes.Length > 0; if (needAppendLine && !appendTypeInfo) { sb.AppendLine(); sb.AppendFormat(" //{0} \r\n", classType.FullName.Replace("+", ".")); appendTypeInfo = true; } if (needAppendLine) { var att = (NbRefFieldAttribute)customAttributes[0]; var fieldValue = new NbRefFieldValue { Description = att.Description, FieldName = fieldInfo.Name }; var value = GetValue(fieldInfo); fieldValue.FieldValue = value.ToString(); // IsLiteral determines if its value is written at // compile time and not changeable // IsInitOnly determine if the field can be set // in the body of the constructor // for C# a field which is readonly keyword would have both true // but a const field would have only IsLiteral equal to true if (fieldInfo.FieldType == typeof(int)) { if (fieldInfo.IsLiteral && !fieldInfo.IsInitOnly) { sb.AppendFormat(" //{0}.{1} = {2}; //{3} //!const or readonly cannot set value! \r\n", classType.FullName.Replace("+", "."), fieldValue.FieldName, fieldValue.FieldValue, fieldValue.Description); } else { sb.AppendFormat(" {0}.{1} = {2}; //{3} \r\n", classType.FullName.Replace("+", "."), fieldValue.FieldName, fieldValue.FieldValue, fieldValue.Description); } } else { if (fieldInfo.IsLiteral && !fieldInfo.IsInitOnly) { sb.AppendFormat(" //{0}.{1} = @\"{2}\"; //{3} //!const or readonly cannot set value! \r\n", classType.FullName.Replace("+", "."), fieldValue.FieldName, fieldValue.FieldValue, fieldValue.Description); } else { sb.AppendFormat(" {0}.{1} = @\"{2}\"; //{3} \r\n", classType.FullName.Replace("+", "."), fieldValue.FieldName, fieldValue.FieldValue, fieldValue.Description); } } } } }
public void GetAllNbRefFieldStrings_should_ok() { //var theType = typeof(Dic_DicTypeCode); //var nbRefFileds = NbRefFieldValue.GetAllNbRefFieldStrings(theType); //foreach (var nbRefFiled in nbRefFileds) //{ // var barValue = DicType.Create(nbRefFiled.FieldValue, nbRefFiled.Description, true, true); // barValue.LogJson(); //} var allNbRefFieldStrings = NbRefFieldValue.GetAllNbRefFieldStrings(typeof(MockCodes)); allNbRefFieldStrings.LogJson(); ShouldOK("A1", "A1", "A1Desc", allNbRefFieldStrings[0]); ShouldOK("X", "B1", "XDesc", allNbRefFieldStrings[1]); ShouldOK("A2", "A2", "A2Desc", allNbRefFieldStrings[2]); }
public static IList <NbRefFieldValue> ExportNbRefFields(params Assembly[] assemblies) { var nbRefFieldValues = new List <NbRefFieldValue>(); foreach (var assembly in assemblies) { var types = assembly.GetTypes(); foreach (var type in types) { var theFieldValues = NbRefFieldValue.GetAllNbRefFieldStrings(type); foreach (var theFieldValue in theFieldValues) { nbRefFieldValues.Add(theFieldValue); } } } return(nbRefFieldValues); }
/// <summary> /// 获取某个类型声明的所有的NbRefFieldAttribute字段的信息 /// </summary> /// <param name="classType"></param> /// <param name="includeRefAndReadOnly"></param> /// <returns></returns> public static IList <NbRefFieldValue> GetAllNbRefFieldStrings(Type classType, bool includeRefAndReadOnly = true) { var list = new List <NbRefFieldValue>(); var fieldInfos = classType.GetFields(); foreach (var fieldInfo in fieldInfos) { var customAttributes = fieldInfo.GetCustomAttributes(typeof(NbRefFieldAttribute), false); if (customAttributes.Length > 0) { // IsLiteral determines if its value is written at // compile time and not changeable // IsInitOnly determine if the field can be set // in the body of the constructor // for C# a field which is readonly keyword would have both true // but a const field would have only IsLiteral equal to true if (!includeRefAndReadOnly) { if (fieldInfo.IsLiteral && !fieldInfo.IsInitOnly) { continue; } } var att = (NbRefFieldAttribute)customAttributes[0]; var fieldValue = new NbRefFieldValue { Description = att.Description, FieldName = fieldInfo.Name, ValueBag = att.ValueBag }; //var value = fieldInfo.GetValue(null); var value = GetValue(fieldInfo); fieldValue.FieldValue = value.ToString(); list.Add(fieldValue); } } return(list); }