Esempio n. 1
0
        public static DataHolderDefinition CreateDataHolderDefinition(Type type)
        {
            if (type.GetInterface("IDataHolder") == null)
            {
                throw new ArgumentException("DataHolder-Type must implement IDataHolder: " + type.FullName);
            }
            DataHolderAttribute attribute =
                ((IEnumerable <DataHolderAttribute>)type.GetCustomAttributes(typeof(DataHolderAttribute), false))
                .FirstOrDefault();
            string dependingField;
            string name;

            if (attribute == null)
            {
                dependingField = null;
                name           = type.Name;
            }
            else
            {
                name           = string.IsNullOrEmpty(attribute.Name) ? type.Name : attribute.Name;
                dependingField = attribute.DependsOnField;
            }

            return(new DataHolderDefinition(name, type, dependingField, attribute));
        }
Esempio n. 2
0
        public DataHolderDefinition(string name, Type type, string dependingField, DataHolderAttribute attribute)
        {
            m_name = name;
            m_DependingFieldName = dependingField;
            m_Attribute          = attribute;
            m_Type = type;
            DependingProducer[] customAttributes = type.GetCustomAttributes <DependingProducer>();
            if (customAttributes.Length == 0)
            {
                m_dependingProducers = null;
            }
            else
            {
                m_dependingProducers = new Dictionary <object, IProducer>();
                foreach (DependingProducer dependingProducer in customAttributes)
                {
                    m_dependingProducers.Add(dependingProducer.Key, dependingProducer.Producer);
                }
            }

            if (type.IsAbstract)
            {
                if (m_dependingProducers == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's Type is abstract and it did not define depending Producers: {0}",
                              (object)type.FullName);
                }
                if (m_DependingFieldName == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's Type is abstract and it did not define the DependsOnField in the DataHolderAttribute: {0}",
                              (object)type.FullName);
                }
            }
            else
            {
                m_defaultProducer = new DefaultProducer(type);
            }

            try
            {
                GetDataFields(type, Fields, null);
                if (type.IsAbstract && m_DependingField == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's DependsOnField (\"{0}\"), as defined in the DataHolderAttribute, does not exist: {1}",
                              (object)m_DependingFieldName, (object)type.FullName);
                }
            }
            catch (Exception ex)
            {
                throw new DataHolderException(ex, "Unable to create DataHolderDefinition for: " + name);
            }
        }
Esempio n. 3
0
        public DataHolderDefinition(string name, Type type, string dependingField, DataHolderAttribute attribute)
        {
            m_name = name;
            m_DependingFieldName = dependingField;
            m_Attribute = attribute;
            m_Type = type;

            var dependingProducerTypes = type.GetCustomAttributes<DependingProducer>();
            if (dependingProducerTypes.Length == 0)
            {
                m_dependingProducers = null;
            }
            else
            {
                m_dependingProducers = new Dictionary<object, IProducer>();
                foreach (var prodAttr in dependingProducerTypes)
                {
                    var key = prodAttr.Key;
                    //var keyType = key.GetType();
                    //if (keyType.IsEnum)
                    //{
                    //    key = Convert.ChangeType(key, Enum.GetUnderlyingType(keyType));
                    //}
                    //if (key is uint)
                    //{
                    //    key = (int)((uint)key);
                    //}
                    //else if (key is ulong)
                    //{
                    //    key = (long)((ulong)key);
                    //}
                    m_dependingProducers.Add(key, prodAttr.Producer);
                }
            }

            if (type.IsAbstract)
            {
                if (m_dependingProducers == null)
                {
                    throw new DataHolderException(
                        "Cannot define DataHolder because it's Type is abstract and it did not define depending Producers: {0}",
                        type.FullName);
                }
                if (m_DependingFieldName == null)
                {
                    throw new DataHolderException(
                        "Cannot define DataHolder because it's Type is abstract and it did not define the DependsOnField in the DataHolderAttribute: {0}",
                        type.FullName);
                }
            }
            else
            {
                m_defaultProducer = new DefaultProducer(type);
            }

            try
            {
                GetDataFields(type, Fields, null);
                if (type.IsAbstract && m_DependingField == null)
                {
                    throw new DataHolderException(
                        "Cannot define DataHolder because it's DependsOnField (\"{0}\"), as defined in the DataHolderAttribute, does not exist: {1}",
                        m_DependingFieldName, type.FullName);
                }
            }
            catch (Exception e)
            {
                throw new DataHolderException(e, "Unable to create DataHolderDefinition for: " + name);
            }
        }
Esempio n. 4
0
        public DataHolderDefinition(string name, Type type, string dependingField, DataHolderAttribute attribute)
        {
            m_name = name;
            m_DependingFieldName = dependingField;
            m_Attribute          = attribute;
            m_Type = type;

            var dependingProducerTypes = type.GetCustomAttributes <DependingProducer>();

            if (dependingProducerTypes.Length == 0)
            {
                m_dependingProducers = null;
            }
            else
            {
                m_dependingProducers = new Dictionary <object, IProducer>();
                foreach (var prodAttr in dependingProducerTypes)
                {
                    var key = prodAttr.Key;
                    //var keyType = key.GetType();
                    //if (keyType.IsEnum)
                    //{
                    //    key = Convert.ChangeType(key, Enum.GetUnderlyingType(keyType));
                    //}
                    //if (key is uint)
                    //{
                    //    key = (int)((uint)key);
                    //}
                    //else if (key is ulong)
                    //{
                    //    key = (long)((ulong)key);
                    //}
                    m_dependingProducers.Add(key, prodAttr.Producer);
                }
            }

            if (type.IsAbstract)
            {
                if (m_dependingProducers == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's Type is abstract and it did not define depending Producers: {0}",
                              type.FullName);
                }
                if (m_DependingFieldName == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's Type is abstract and it did not define the DependsOnField in the DataHolderAttribute: {0}",
                              type.FullName);
                }
            }
            else
            {
                m_defaultProducer = new DefaultProducer(type);
            }

            try
            {
                GetDataFields(type, Fields, null);
                if (type.IsAbstract && m_DependingField == null)
                {
                    throw new DataHolderException(
                              "Cannot define DataHolder because it's DependsOnField (\"{0}\"), as defined in the DataHolderAttribute, does not exist: {1}",
                              m_DependingFieldName, type.FullName);
                }
            }
            catch (Exception e)
            {
                throw new DataHolderException(e, "Unable to create DataHolderDefinition for: " + name);
            }
        }