Beispiel #1
0
        public static void TestCustomAttributeResolover()
        {
            UserDto user = new UserDto
            {
                Name  = "qingluhuang",
                Hobby = "body building",
                Test  = new TestClass
                {
                    Location = new Location
                    {
                        Region   = "china",
                        City     = "xiamen",
                        Address  = "honglian",
                        SubTitle = new InnerTestClass
                        {
                            SubTitle = "test1"
                        }
                    },
                    OptionalLocation = new Location
                    {
                        Region  = "china",
                        City    = "sanming",
                        Address = "youxi"
                    }
                }
            };

            ICustomAttributeResolver resolver = new CustomAttributeResolver();
            var customAttribute = resolver.ResolveCustomAttribute(user);
            int i = 0;
        }
        public MetadataSystem(MetadataReader reader)
        {
            _reader = reader;

            _entryPoint = new Lazy <MethodDefinition>(() =>
                                                      GetMethodDefinition(_reader.EntryPoint.RID));

            _assembly = new Lazy <AssemblyDefinition>(() =>
                                                      AssemblyDefinitionResolver.Resolve(_reader.AssemblyTable.Row, this));

            _module = new Lazy <ModuleDefinition>(() =>
                                                  ModuleDefinitionResolver.Resolve(_reader.ModuleTable.Row, this));

            _assemblyRefs = new Lazy <AssemblyReference[]>(() =>
                                                           _reader.AssemblyRefTable
                                                           .Select((row, id) => AssemblyReferenceResolver.Resolve((uint)(id + 1), row, this))
                                                           .ToArray(_reader.AssemblyRefTable.Length));

            _types = new Lazy <TypeDefinition[]>(() =>
                                                 _reader.TypeDefTable
                                                 .Select((row, id) => TypeDefinitionResolver.Resolve((uint)(id + 1), row, this))
                                                 .ToArray(_reader.TypeDefTable.Length));

            _methods = new Lazy <MethodDefinition[]>(() =>
                                                     _types.Value.SelectMany(type => type.Methods)
                                                     .ToArray(_reader.MethodTable.Length));

            _fields = new Lazy <FieldDefinition[]>(() =>
                                                   _types.Value.SelectMany(type => type.Fields)
                                                   .ToArray(_reader.FieldTable.Length));

            _typeRefLookup = new Lazy <ILookup <MetadataToken, TypeReference> >(() =>
                                                                                _reader.TypeRefTable.ToLookup(row => row.ResolutionScope,
                                                                                                              row => TypeReferenceResolver.Resolve(row, this)));

            _memberRefLookup = new Lazy <ILookup <MetadataToken, IMemberReference> >(() =>
                                                                                     _reader.MemberRefTable.ToLookup(row => row.Class,
                                                                                                                     row => MemberReferenceResolver.Resolve(row, this)));

            _attributeLookup = new Lazy <ILookup <MetadataToken, CustomAttribute> >(() =>
                                                                                    _reader.CustomAttributeTable.ToLookup(row => row.Parent,
                                                                                                                          row => CustomAttributeResolver.Resolve(row, this)));

            _typeRefs = new Lazy <TypeReference[]>(() =>
                                                   _typeRefLookup.Value.SelectMany(items => items)
                                                   .ToArray(_reader.TypeRefTable.Length));

            _memberRefs = new Lazy <IMemberReference[]>(() =>
                                                        _memberRefLookup.Value.SelectMany(items => items)
                                                        .ToArray(_reader.MemberRefTable.Length));

            _attributes = new Lazy <CustomAttribute[]>(() =>
                                                       _attributeLookup.Value.SelectMany(items => items)
                                                       .ToArray(_reader.CustomAttributeTable.Length));

            _enclosingTypeMapping = new Lazy <Dictionary <MetadataToken, ITypeInfo> >(() =>
                                                                                      _reader.NestedClassTable.ToDictionary(
                                                                                          row => row.NestedClass,
                                                                                          row => GetTypeInfo(row.EnclosingClass)));
        }