当前位置: 首页>>代码示例>>C#>>正文


C# TypeBuilder.DefineOverride方法代码示例

本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineOverride方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineOverride方法的具体用法?C# TypeBuilder.DefineOverride怎么用?C# TypeBuilder.DefineOverride使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.TypeBuilder的用法示例。


在下文中一共展示了TypeBuilder.DefineOverride方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ImplementCreateProperties

        private void ImplementCreateProperties(CompilationContext ctx, TypeBuilder t, IBranch root)
        {
            var relevantCumulations = ctx.ScheduledCumulations.ContainsKey(root) ?
                ctx.ScheduledCumulations[root].Where(co => co.Subject.IsFov() || co.Subject.Parent.IsFov()) : null;
            var special = ctx.CumulativeCompilation && relevantCumulations != null;
            if (special && relevantCumulations.IsEmpty()) return;

            var base_cp = t.BaseType.GetMethod("CreateProperties", BF.All);
            var cp = t.DefineOverride(base_cp);
            if (!base_cp.IsAbstract) cp.il().ldarg(0).call(base_cp);
            ctx.CPs[t] = cp;

            var svd = root.GetBranches().Where(b => b.Name == "_sourceValueDeclarations");
            var formulae = root.GetBranches().Where(b => b.Name == "_formulaDeclarations");
            var conditions = root.GetBranches().Where(b => b.Name == "_conditions");
            var nodes = root.GetBranches().Except(svd).Except(formulae).Except(conditions);
            var svdAndFormulae = svd.Concat(formulae).SelectMany(b => b.GetBranches()).ToArray();
            if (special) svdAndFormulae = svdAndFormulae.Where(b => 
                relevantCumulations.Any(co => co.Subject == b || co.Subject.Parent == b)).ToArray();

            var propGetCache = new Dictionary<VPath, MethodInfo>();
            var fieldCache = new Dictionary<VPath, FieldBuilder>();
            var nameCache = new HashSet<String>();
            Action<IBranch, bool> ensureProperty = (b, external) =>
            {
                if (propGetCache.ContainsKey(b.VPath))
                {
                    return;
                }
                else
                {
                    var typeToken = b.GetValue("type").ContentString;
                    var propType = typeToken.GetTypeFromToken();

                    var baseProp = t.BaseType.GetProperties(BF.All).SingleOrDefault(
                        p => p.HasAttr<VPathAttribute>() && p.Attr<VPathAttribute>().VPath == b.VPath);
                    var basePropOk = baseProp != null && baseProp.PropertyType == propType;

                    String name;
                    if (basePropOk)
                    {
                        name = baseProp.Name;
                    }
                    else
                    {
                        var desiredName = b.GetPropertyName();
                        name = desiredName;

                        var i = 0;
                        while (nameCache.Contains(name))
                        {
                            name = desiredName + "~" + ++i;
                        }

                        nameCache.Add(b.GetPropertyName());
                    }

                    if (external)
                    {
                        if (basePropOk)
                        {
                            propGetCache.Add(b.VPath, baseProp.GetGetMethod(true));
                        }
                        else
                        {
                            var p_prop = t.DefineProperty(name, PropA.None, propType, new Type[0]);
                            p_prop.SetCustomAttribute(new CustomAttributeBuilder(
                                typeof(VPathAttribute).GetConstructor(typeof(String).MkArray()),
                                b.VPath.ToString().MkArray()));

                            var get = t.DefineMethod("get_" + name, MA.ProtectedProp, propType, new Type[0]);
                            get.il()
                               .ldarg(0)
                               .callvirt(typeof(CompiledNode).GetProperty("Root").GetGetMethod())
                               .ldstr(b.VPath.ToString())
                               .newobj(typeof(VPath), typeof(String))
                               .callvirt(typeof(ICompiledNode).GetMethod("Eval"))
                               .ret();

                            p_prop.SetGetMethod(get);
                            propGetCache.Add(b.VPath, get);
                        }
                    }
                    else
                    {
                        var p_prop = t.DefineProperty(name, PropA.None, propType, new Type[0]);
                        p_prop.SetCustomAttribute(new CustomAttributeBuilder(
                            typeof(VPathAttribute).GetConstructor(typeof(String).MkArray()),
                            b.VPath.ToString().MkArray()));

                        MethodBuilder get;
                        if (basePropOk)
                        {
                            var baseGet = baseProp.GetGetMethod(true);
                            get = t.DefineOverride(baseGet);
                            p_prop.SetGetMethod(get);
                        }
                        else
                        {
                            get = t.DefineMethod("get_" + name, MA.PublicProp, propType, new Type[0]);
//.........这里部分代码省略.........
开发者ID:xeno-by,项目名称:elf4b,代码行数:101,代码来源:VaultCompiler.cs

示例2: ImplementCreateChildren

        private void ImplementCreateChildren(CompilationContext ctx, TypeBuilder t, IBranch root)
        {
            var relevantCumulations = ctx.ScheduledCumulations.ContainsKey(root) ? 
                ctx.ScheduledCumulations[root].Where(co => !(co.Subject.IsFov() || co.Subject.Parent.IsFov())) : null;
            var special = ctx.CumulativeCompilation && relevantCumulations != null;
            if (special && relevantCumulations.IsEmpty()) return;

            var base_cc = t.BaseType.GetMethod("CreateChildren", BF.All);
            var cc = t.DefineOverride(base_cc);
            if (!base_cc.IsAbstract) cc.il().ldarg(0).call(base_cc);
            ctx.CCs[t] = cc;

            var svd = root.GetBranches().Where(b => b.Name == "_sourceValueDeclarations");
            var formulae = root.GetBranches().Where(b => b.Name == "_formulaDeclarations");
            var conditions = root.GetBranches().Where(b => b.Name == "_conditions");
            var nodes = root.GetBranches().Except(svd).Except(formulae).Except(conditions).ToArray();
            if (special) nodes = nodes.Where(b => relevantCumulations.Any(co => co.Subject == b)).ToArray();

            foreach (var b in nodes)
            {
                var b_type = CompileNode(ctx, b);

                var f_child = t.DefineField("_" + b.GetPropertyName().ToLower(), b_type, FA.Private);
                var p_child = t.DefineProperty(b.GetPropertyName(), PropA.None, b_type, new Type[0]);
                p_child.SetCustomAttribute(new CustomAttributeBuilder(
                    typeof(VPathAttribute).GetConstructor(typeof(String).MkArray()),
                    b.VPath.ToString().MkArray()));

                var get = t.DefineMethod("get_" + b.GetPropertyName(), MA.PublicProp, b_type, new Type[0]);
                p_child.SetGetMethod(get);

                Label nodeWasCreatedByTheFactory;
                get.il()
                   .ldarg(0)
                   .ldfld(f_child)

                   // here we check whether the factory returned a valid (non-null) node
                   // if the latter is false, then the node has been deleted during cumulative recompilation
                   // and we need to crash with the same message as CompiledNode.Child(vpath)
                   .def_label(out nodeWasCreatedByTheFactory)
                   .brtrue(nodeWasCreatedByTheFactory)
                   .ldstr(String.Format("There's no compiled node at VPath '{0}'.", b.VPath))
                   [email protected](typeof(NotImplementedException), typeof(String))

                   .label(nodeWasCreatedByTheFactory)
                   .ldarg(0)
                   .ldfld(f_child)
                   .ret();

                cc.il()
                  .ldarg(0)
                  .ldarg(0)
                  .callvirt(typeof(CompiledNode).GetProperty("Root", BF.All).GetGetMethod(true))
                  .ldstr(b.VPath.ToString())
                  .newobj(typeof(VPath), typeof(String))
                  .ldarg(0)
                  .callvirt(typeof(CompiledScenario).GetMethod("CreateNode", BF.All))
                  .stfld(f_child)
                  .ldarg(0)
                  .callvirt(typeof(CompiledNode).GetProperty("Children").GetGetMethod())
                  .ldarg(0)
                  .ldfld(f_child)
                  .callvirt(typeof(CompiledNodeCollection).GetMethod("Add"));
            }

            cc.il().ret();
        }
开发者ID:xeno-by,项目名称:elf4b,代码行数:67,代码来源:VaultCompiler.cs


注:本文中的System.Reflection.Emit.TypeBuilder.DefineOverride方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。