Beispiel #1
0
        public void ProcessField(SysReflection.FieldInfo aField)
        {
            string xFieldName = LabelName.GetFullName(aField);
            xFieldName = DataMember.GetStaticFieldName(aField);
            if (Cosmos.Assembler.Assembler.CurrentInstance.DataMembers.Count(x => x.Name == xFieldName) == 0)
            {
                var xItemList = (from item in aField.GetCustomAttributes(false)
                                 where item.GetType().FullName == "ManifestResourceStreamAttribute"
                                 select item).ToList();

                object xItem = null;
                if (xItemList.Count > 0)
                    xItem = xItemList[0];
                string xManifestResourceName = null;
                if (xItem != null)
                {
                    var xItemType = xItem.GetType();
                    xManifestResourceName = (string)xItemType.GetField("ResourceName").GetValue(xItem);
                }
                if (xManifestResourceName != null)
                {
                    // todo: add support for manifest streams again
                    //string xFileName = Path.Combine(mOutputDir,
                    //                                (xCurrentField.DeclaringType.Assembly.FullName + "__" + xManifestResourceName).Replace(",",
                    //                                                                                                                       "_") + ".res");
                    var xTarget = new StringBuilder();
                    using (var xStream = aField.DeclaringType.Assembly.GetManifestResourceStream(xManifestResourceName))
                    {
                        if (xStream == null)
                        {
                            throw new Exception("Resource '" + xManifestResourceName + "' not found!");
                        }
                        xTarget.Append("0,");
                        // todo: abstract this array code out.
                        xTarget.Append((uint)InstanceTypeEnum.StaticEmbeddedArray);
                        xTarget.Append(",");
                        xTarget.Append((int)xStream.Length);
                        xTarget.Append(",");
                        xTarget.Append("1,");
                        while (xStream.Position < xStream.Length)
                        {
                            xTarget.Append(xStream.ReadByte());
                            xTarget.Append(",");
                        }
                        xTarget.Append(",");
                    }

                    Assembler.DataMembers.Add(new DataMember("___" + xFieldName + "___Contents",
                                                              "db",
                                                              xTarget));
                    Assembler.DataMembers.Add(new DataMember(xFieldName,
                                                              "dd",
                                                              "___" + xFieldName + "___Contents"));
                }
                else
                {
                    uint xTheSize;
                    //string theType = "db";
                    Type xFieldTypeDef = aField.FieldType;
                    if (!xFieldTypeDef.IsClass || xFieldTypeDef.IsValueType)
                    {
                        xTheSize = ILOp.SizeOfType(aField.FieldType);
                    }
                    else
                    {
                        xTheSize = 8;
                    }
                    byte[] xData = new byte[xTheSize];
                    try
                    {
                        object xValue = aField.GetValue(null);
                        if (xValue != null)
                        {
                            try
                            {
                                Type xTyp = xValue.GetType();
                                if (xTyp.IsEnum)
                                {
                                    xValue = Convert.ChangeType(xValue, Enum.GetUnderlyingType(xTyp));
                                }
                                if (xTyp.IsValueType)
                                {
                                    for (int x = 4; x < xTheSize; x++)
                                    {
                                        xData[x] = Marshal.ReadByte(xValue,
                                                                    x);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    catch
                    {
                    }
                    Cosmos.Assembler.Assembler.CurrentInstance.DataMembers.Add(new DataMember(xFieldName, xData));
                }
            }
        }