當前位置: 首頁>>代碼示例>>C#>>正文


C# PropertyDefinition.IsNullable方法代碼示例

本文整理匯總了C#中Mono.Cecil.PropertyDefinition.IsNullable方法的典型用法代碼示例。如果您正苦於以下問題:C# PropertyDefinition.IsNullable方法的具體用法?C# PropertyDefinition.IsNullable怎麽用?C# PropertyDefinition.IsNullable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Mono.Cecil.PropertyDefinition的用法示例。


在下文中一共展示了PropertyDefinition.IsNullable方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: WeaveProperty

    private WeaveResult WeaveProperty(PropertyDefinition prop, TypeDefinition type, Dictionary<string, Tuple<MethodReference, MethodReference>> methodTable)
    {
        var columnName = prop.Name;
        var mapToAttribute = prop.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "MapToAttribute");
        if (mapToAttribute != null)
        {
            columnName = (string)mapToAttribute.ConstructorArguments[0].Value;
        }

        var backingField = prop.GetBackingField();
        var isIndexed = prop.CustomAttributes.Any(a => a.AttributeType.Name == "IndexedAttribute");
        if (isIndexed && (!_indexableTypes.Contains(prop.PropertyType.FullName)))
        {
            return WeaveResult.Error($"{type.Name}.{prop.Name} is marked as [Indexed] which is only allowed on integral types as well as string, bool and DateTimeOffset, not on {prop.PropertyType.FullName}.");
        }

        var isPrimaryKey = prop.IsPrimaryKey();
        if (isPrimaryKey && (!_primaryKeyTypes.Contains(prop.PropertyType.FullName)))
        {
            return WeaveResult.Error($"{type.Name}.{prop.Name} is marked as [PrimaryKey] which is only allowed on integral and string types, not on {prop.PropertyType.FullName}.");
        }

        var isRequired = prop.IsRequired();
        if (isRequired && 
            !prop.IsNullable() &&
            prop.PropertyType.FullName != StringTypeName && 
            prop.PropertyType.FullName != ByteArrayTypeName)
        {
            return WeaveResult.Error($"{type.Name}.{prop.Name} is marked as [Required] which is only allowed on strings or nullable scalar types, not on {prop.PropertyType.FullName}.");
        }

        if (!prop.IsAutomatic())
        {
            if (prop.PropertyType.Resolve().BaseType.IsSameAs(_references.RealmObject))
            {
                return WeaveResult.Warning($"{type.Name}.{columnName} is not an automatic property but its type is a RealmObject which normally indicates a relationship.");
            }

            return WeaveResult.Skipped();
        }

        if (_typeTable.ContainsKey(prop.PropertyType.FullName))
        {
            // If the property is automatic but doesn't have a setter, we should still ignore it.
            if (prop.SetMethod == null)
            {
                return WeaveResult.Skipped();
            }

            var typeId = prop.PropertyType.FullName + (isPrimaryKey ? " unique" : string.Empty);
            Tuple<MethodReference, MethodReference> realmAccessors;
            if (!methodTable.TryGetValue(typeId, out realmAccessors))
            {
                var getter = new MethodReference("Get" + _typeTable[prop.PropertyType.FullName] + "Value", prop.PropertyType, _references.RealmObject)
                {
                    HasThis = true,
                    Parameters = { new ParameterDefinition(ModuleDefinition.TypeSystem.String) }
                };
                var setter = new MethodReference("Set" + _typeTable[prop.PropertyType.FullName] + "Value" + (isPrimaryKey ? "Unique" : string.Empty), ModuleDefinition.TypeSystem.Void, _references.RealmObject)
                {
                    HasThis = true,
                    Parameters =
                    {
                        new ParameterDefinition(ModuleDefinition.TypeSystem.String),
                        new ParameterDefinition(prop.PropertyType)
                    }
                };

                methodTable[typeId] = realmAccessors = Tuple.Create(getter, setter);
            }

            ReplaceGetter(prop, columnName, realmAccessors.Item1);
            ReplaceSetter(prop, backingField, columnName, realmAccessors.Item2);
        }
        else if (prop.IsIList())
        {
            var elementType = ((GenericInstanceType)prop.PropertyType).GenericArguments.Single();
            if (!elementType.Resolve().BaseType.IsSameAs(_references.RealmObject))
            {
                return WeaveResult.Warning($"SKIPPING {type.Name}.{columnName} because it is an IList but its generic type is not a RealmObject subclass, so will not persist.");
            }

            if (prop.SetMethod != null)
            {
                return WeaveResult.Error($"{type.Name}.{columnName} has a setter but its type is a IList which only supports getters.");
            }

            var concreteListConstructor = _references.System_Collections_Generic_ListOfT_Constructor.MakeHostInstanceGeneric(elementType);

            // weaves list getter which also sets backing to List<T>, forcing it to accept us setting it post-init
            var backingDef = backingField as FieldDefinition;
            if (backingDef != null)
            {
                backingDef.Attributes &= ~FieldAttributes.InitOnly;  // without a set; auto property has this flag we must clear
            }

            ReplaceListGetter(prop, backingField, columnName,
                              new GenericInstanceMethod(_references.RealmObject_GetListValue) { GenericArguments = { elementType } },
                              concreteListConstructor);
        }
//.........這裏部分代碼省略.........
開發者ID:realm,項目名稱:realm-dotnet,代碼行數:101,代碼來源:ModuleWeaver.cs


注:本文中的Mono.Cecil.PropertyDefinition.IsNullable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。