public FieldInfoMatchTarget(TypeInfo parentTypeInfo, int index) { if (parentTypeInfo == null) throw new ArgumentNullException(nameof(parentTypeInfo)); m_parentTypeInfo = parentTypeInfo; m_index = index; m_varDesc = m_parentTypeInfo.GetVarDesc(m_index); m_nativeParentTypeName = m_parentTypeInfo.GetDocumentation(); }
/// <summary> /// Test whether the specified VT_RECORD contains any field that can be converted to a managed reference type /// </summary> private bool HasObjectFields(TypeInfo typeInfo) { TypeAttr typeAttr = typeInfo.GetTypeAttr(); for (int i = 0; i < typeAttr.cVars; ++i) { VarDesc var = typeInfo.GetVarDesc(i); if (IsObjectType(typeInfo, var.elemdescVar.tdesc)) return true; } return false; }
public static bool HasNewEnumMember(ConverterInfo info, TypeInfo typeInfo, string fullName) { bool hasNewEnumMember = false; bool hasDuplicateNewEnumMember = false; int firstNewEnum = -1; using (TypeAttr attr = typeInfo.GetTypeAttr()) { if (attr.IsDispatch || (attr.IsInterface && ConvCommon.IsDerivedFromIDispatch(typeInfo))) { // Check to see if the interface has a function with a DISPID of DISPID_NEWENUM. for (int i = 0; i < attr.cFuncs; ++i) { using(FuncDesc func = typeInfo.GetFuncDesc(i)) { if (IsNewEnumFunc(info, typeInfo, func, i)) { if (!hasNewEnumMember) firstNewEnum = func.memid; if (hasNewEnumMember) hasDuplicateNewEnumMember = true; // The interface has a function with a DISPID of DISPID_NEWENUM. hasNewEnumMember = true; } } } // Check to see if the interface as a property with a DISPID of DISPID_NEWENUM. for (int i = 0; i < attr.cVars; ++i) { using (VarDesc varDesc = typeInfo.GetVarDesc(i)) { if (IsNewEnumDispatchProperty(info, typeInfo, varDesc, i)) { if (!hasNewEnumMember) firstNewEnum = varDesc.memid; if (hasNewEnumMember) hasDuplicateNewEnumMember = true; // The interface has a property with a DISPID of DISPID_NEWENUM. hasNewEnumMember = true; } } } // Check to see if the ForceIEnumerable custom value exists on the type if (HasForceIEnumerableCustomAttribute(typeInfo)) hasNewEnumMember = true; if (hasDuplicateNewEnumMember) { info.ReportEvent( WarningCode.Wrn_MultiNewEnum, Resource.FormatString("Wrn_MultiNewEnum", fullName, typeInfo.GetDocumentation(firstNewEnum))); } } else { // Check to see if the ForceIEnumerable custom value exists on the type // If it does, spit out a warning. if (HasForceIEnumerableCustomAttribute(typeInfo)) { string msg = Resource.FormatString( "Wrn_IEnumCustomAttributeOnIUnknown", CustomAttributeGuids.GUID_ForceIEnumerable.ToString().ToUpper(), typeInfo.GetDocumentation()); info.ReportEvent(WarningCode.Wrn_IEnumCustomAttributeOnIUnknown, msg); } } } return hasNewEnumMember; }
/// <summary> /// Create the constant fields on the TypeBuilder according to the VarDesc in the type /// </summary> public static void CreateConstantFields(ConverterInfo info, TypeInfo type, TypeBuilder typeBuilder, ConvType convType) { using (TypeAttr attr = type.GetTypeAttr()) { int cVars = attr.cVars; for (int n = 0; n < cVars; ++n) { using (VarDesc var = type.GetVarDesc(n)) { string fieldName = type.GetDocumentation(var.memid); // We don't want the same conversion rules as Field for VT_BOOL and VT_ARRAY, so use Element instead // Basically Element is the same as Field except that it doesn't have special rules for VT_BOOL/VT_ARRAY TypeConverter typeConverter = new TypeConverter(info, type, var.elemdescVar.tdesc, ConversionType.Element); if (typeConverter.ConvertedType == typeof(DateTime)) typeConverter = new TypeConverter(typeof(float)); Type fieldType = typeConverter.ConvertedType; if (typeConverter.IsConversionLoss) { // // Emit Wrn_UnconvertableField warning // info.ReportEvent( WarningCode.Wrn_UnconvertableField, Resource.FormatString("Wrn_UnconvertableField", typeBuilder.FullName, fieldName)); } Type targetType; if (convType == ConvType.Enum) targetType = typeBuilder; // use enum type as the field type for enum else targetType = fieldType; // use the real type as the field type FieldBuilder fieldBuilder = typeBuilder.DefineField(fieldName, targetType, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal); typeConverter.ApplyAttributes(fieldBuilder); // // Emit TypeLibVarAttribute if necessary // if (var.wVarFlags != 0) { fieldBuilder.SetCustomAttribute(CustomAttributeHelper.GetBuilderForTypeLibVar((TypeLibVarFlags)var.wVarFlags)); } // Emit constant for static fields ConvCommon.SetDefaultValue(var, fieldType, fieldBuilder); } } } }