Ejemplo n.º 1
0
        protected MetricUnit()
        {
            //access the SIUnitAttribute
            MemberInfo info = this.GetType();

            object[] attributes = (object[])info.GetCustomAttributes(true);

            //get the UnitAttribute
            MetricUnitAttribute siua = (MetricUnitAttribute)attributes.SingleOrDefault <object>(ut => ut is MetricUnitAttribute);

            if (siua != null)
            {
                defaultUnitPrefix = MetricPrefix.FromPrefixName(siua.SiPrefix.ToString());
                unitPrefix        = MetricPrefix.FromPrefixName(siua.SiPrefix.ToString());
            }
            else
            {
                throw new UnitException("SIUnitAttribute Not Found");
            }


            //ReferenceUnit attribute may or may not appear
            // if it appears then the unit is not a default SI unit
            // but act as SI Unit {means take prefixes}.  and accepted by the SI poids
            // however the code of reference unit is in the base class code.
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the default unit type of quantity type parameter based on the unit system (namespace)
        /// under the Units name space.
        /// The Default Unit Type for length in Imperial is Foot for example.
        /// </summary>
        /// <param name="quantityType">quantity type</param>
        /// <param name="unitSystem">The Unit System or explicitly the namespace under Units Namespace</param>
        /// <returns>Unit Type Based on the unit system</returns>
        public static Type GetDefaultUnitTypeOf(Type quantityType, string unitSystem)
        {
            unitSystem = unitSystem.ToUpperInvariant();

            if (unitSystem.Contains("METRIC.SI"))
            {
                Type oUnitType = GetDefaultSIUnitTypeOf(quantityType);
                return(oUnitType);
            }
            else
            {
                //getting the generic type
                if (!quantityType.IsGenericTypeDefinition)
                {
                    //the passed type is AnyQuantity<object> for example
                    //I want to get the type without type parameters AnyQuantity<>

                    quantityType = quantityType.GetGenericTypeDefinition();
                }


                //predictor of default unit.
                Func <Type, bool> SearchForQuantityType = unitType =>
                {
                    //search in the attributes of the unit type
                    MemberInfo info = unitType as MemberInfo;

                    object[] attributes = (object[])info.GetCustomAttributes(true);

                    //get the UnitAttribute
                    UnitAttribute ua = (UnitAttribute)attributes.SingleOrDefault <object>(ut => ut is UnitAttribute);

                    if (ua != null)
                    {
                        if (ua.QuantityType == quantityType)
                        {
                            if (ua is DefaultUnitAttribute)
                            {
                                //explicitly default unit.
                                return(true);
                            }
                            else if (ua is MetricUnitAttribute)
                            {
                                //check if the unit has SystemDefault flag true or not.
                                MetricUnitAttribute mua = ua as MetricUnitAttribute;
                                if (mua.SystemDefault)
                                {
                                    return(true);
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                            else
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                };


                string CurrentUnitSystem = unitSystem; //
                Type   SystemUnitType    = null;

                //search in upper namespaces also to get the default unit of the parent system.
                while (string.IsNullOrEmpty(CurrentUnitSystem) == false && SystemUnitType == null)
                {
                    //prepare the query that we will search in
                    var SystemUnitTypes = from ut in UnitTypes
                                          where ut.Namespace.ToUpperInvariant().EndsWith(CurrentUnitSystem, StringComparison.Ordinal) ||
                                          ut.Namespace.ToUpperInvariant().EndsWith("SHARED", StringComparison.Ordinal)
                                          select ut;

                    //select the default by predictor from the query
                    SystemUnitType = SystemUnitTypes.SingleOrDefault(
                        SearchForQuantityType
                        );

                    if (CurrentUnitSystem.LastIndexOf('.') < 0)
                    {
                        CurrentUnitSystem = "";
                    }
                    else
                    {
                        CurrentUnitSystem = CurrentUnitSystem.Substring(0, CurrentUnitSystem.LastIndexOf('.'));
                    }
                }

                if (SystemUnitType == null && unitSystem.Contains("METRIC"))
                {
                    //try another catch for SI unit for this quantity
                    //   because SI and metric units are disordered for now
                    // so if the search of unit in parent metric doesn't exist then search for it in SI units.

                    SystemUnitType = GetDefaultSIUnitTypeOf(quantityType);
                }

                return(SystemUnitType);
            }
        }