示例#1
0
        public PropertyDescriptor GetIdDescriptor(Type targetType)
        {
			if (_CachedIDs.ContainsKey (targetType))
				return (PropertyDescriptor) _CachedIDs [targetType];
			
			foreach (PropertyInfo pi in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                PersistentIdAttribute[] attrs = pi.GetCustomAttributes(
                    typeof(PersistentIdAttribute), false) as PersistentIdAttribute[];

                if (attrs != null && attrs.Length > 0)
				{
					PersistentIdAttribute pia = attrs[0];
					PropertyDescriptor pid = new PropertyDescriptor(pia.FieldName, pi.Name, DescriptorHelper.IsEntity(pi.PropertyType), DescriptorHelper.IsList(pi.PropertyType), DescriptorHelper.IsGenericList(pi.PropertyType), false, pi.PropertyType);
					_CachedIDs.Add(targetType, pid);

					return pid;
				}
            }

            return null;
        }
示例#2
0
        public PropertyDescriptor GetIdDescriptor_old(Type targetType)
        {
            _RWL.AcquireReaderLock();

            try
            {
                if (_CachedIDs.ContainsKey(targetType))
                    return (PropertyDescriptor)_CachedIDs[targetType];
            }
            finally
            {
                _RWL.ReleaseReaderLock();
            }

            _RWL.AcquireWriterLock();

            try
            {

                foreach (PropertyInfo pi in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    FieldInfo fi = null;

                    //  Try to get an attribute for this property
                    PersistentIdAttribute[] attrs = pi.GetCustomAttributes(
                        typeof(PersistentIdAttribute), false) as PersistentIdAttribute[];

                    // Add this property if more than one Attribute
                    if (attrs != null && attrs.Length > 0)
                    {
                        // Takes the first attribute even if more are set
                        PersistentIdAttribute pia = attrs[0];

                        if (pia.FieldName != null && pia.FieldName != string.Empty)
                            fi = targetType.GetField(pia.FieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    }

                    if (fi == null)
                    {
                        string propertyname = String.Concat(targetType.Name, "Id");
                        if (pi.Name != propertyname && pi.Name != "Id")
                            continue;
                        else
                            if (pi.Name == "Id")
                                propertyname = "Id";

                        // Searches for a Field with the default id name but a different case
                        fi = targetType.GetField(propertyname, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);

                        // If no field was found, searches for _{TypeName}Id
                        if (fi == null)
                            fi = targetType.GetField("_" + propertyname, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);

                        // If no field was found, searches for m_{TypeName}Id
                        if (fi == null)
                            fi = targetType.GetField("m_" + propertyname, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);

                        // If no field was found, searches for <{PropertyName}>k__BackingField (used by the C# 3.0 compiler for syntactic properties)
                        if (fi == null)
                            fi = targetType.GetField("<" + pi.Name + ">k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    }

                    if (fi != null)
                    {
                        PropertyDescriptor pid = new PropertyDescriptor(fi.Name, pi.Name, DescriptorHelper.IsEntity(fi.FieldType), DescriptorHelper.IsList(fi.FieldType), DescriptorHelper.IsGenericList(fi.FieldType), false, fi.FieldType, fi.IsPrivate);
                        _CachedIDs.Add(targetType, pid);
                        return pid;
                    }
                }
            }
            finally
            {
                _RWL.ReleaseWriterLock();
            }

            if (targetType.BaseType != null)
                return GetIdDescriptor_old(targetType.BaseType);

            //If no Id property / _Id field found
            return null;
        }