无法找到或装载组件&QUOT

无法找到或装载组件&QUOT

本文介绍了无法找到或装载组件" tmpAssembly,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一个dinamycally生成的类型为一体的业务规则编辑器的源代码名为codeeffects网站(www。codeeffects.com),但我得到这个例外

 无法找到或装载组件tmpAssembly,版本= 0.0.0.0,文化=中立,公钥=空。错误信息:无法加载文件或程序'tmpAssembly,版本= 0.0.0.0,文化=中性公钥= null或它的一个依赖。该系统找不到指定的文件。 (错误S103)

在控制器中的索引操作是:

  [HTTPGET]
        公众的ActionResult指数()
        {
            IMyInterface的myObject的=(IMyInterface的)ObjectBuilder.CreateOurNewObject();
            类型T = myObject.GetType();
            ViewBag.Rule = RuleModel.Create(T);
            返回查看();
        }

和创建新对象的方法。

 公共静态对象CreateOurNewObject()
        {
            字符串_xml =<根和GT; +
                <列名= \\名称\\>&米伦LT; /列>中+
                <列名= \\名字\\>&艾布拉姆森LT; /列>中+
                <列名= \\博客\\> www.blog.mironabramson.com< /列>中+
                < /根>中;            XmlDocument的xmlDoc中=新的XmlDocument();
            xmlDoc.LoadXml(_xml);            //创建一个动态组装和模块
            的AssemblyName的AssemblyName =新的AssemblyName();
            assemblyName.Name =tmpAssembly;
            。System.Reflection.Emit.AssemblyBuilder assemblyBuilder = Thread.GetDomain()DefineDynamicAssembly(的AssemblyName,AssemblyBuilderAccess.Run);
            ModuleBuilder模块= assemblyBuilder.DefineDynamicModule(tmpModule);            //创建一个新类型构建
            TypeBuilder typeBuilder = module.DefineType(BindableRowCellCollection,TypeAttributes.Public | TypeAttributes.Class);            typeBuilder.AddInterfaceImplementation(typeof运算(IMyInterface的));            //循环将被用来作为在出新型的属性名称的属性
            的foreach(在xmlDoc.SelectSingleNode(XmlNode的节点根)。子节点)
            {
                。字符串参数propertyName = node.Attributes [名称]值;                //生成一个私有字段
                FieldBuilder场= typeBuilder.DefineField(_+ propertyName的,typeof运算(字符串),FieldAttributes.Private);
                //生成一个公共属性
                PropertyBuilder财产=
                    typeBuilder.DefineProperty(propertyName的,
                                     PropertyAttributes.None,
                                     typeof运算(字符串),
                                     新类型[] {typeof运算(字符串)});                //属性集和属性来获取方法需要一套特殊属性:                MethodAttributes GetSetAttr =
                    MethodAttributes.Public |
                    MethodAttributes.HideBySig;                //定义获取访问方法当前私有字段。
                MethodBuilder currGetPropMthdBldr =
                    typeBuilder.DefineMethod(的get_value
                                               GetSetAttr,
                                               typeof运算(字符串),
                                               Type.EmptyTypes);                //中间语言的东西...
                的ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
                currGetIL.Emit(欧普codes.Ldarg_0);
                currGetIL.Emit(欧普codes.Ldfld,场);
                currGetIL.Emit(欧普codes.Ret);                //定义设置存取方法目前的私人领域。
                MethodBuilder currSetPropMthdBldr =
                    typeBuilder.DefineMethod(SET_VALUE
                                               GetSetAttr,
                                               空值,
                                               新类型[] {typeof运算(字符串)});                //又有些中间语言的东西...
                的ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
                currSetIL.Emit(欧普codes.Ldarg_0);
                currSetIL.Emit(欧普codes.Ldarg_1);
                currSetIL.Emit(欧普codes.Stfld,场);
                currSetIL.Emit(欧普codes.Ret);                //最后,我们必须在上面创建我们PropertyBuilder两个方法映射
                //其相应的行为,获取和set分别。
                property.SetGetMethod(currGetPropMthdBldr);
                property.SetSetMethod(currSetPropMthdBldr);
            }            //生成我们的类型
            键入generetedType = typeBuilder.CreateType();            //现在我们有我们的类型。让我们从它创建一个实例:
            反对generetedObject = Activator.CreateInstance(generetedType);            //遍历所有生成的属性,并指定由我们的XML值:
            的PropertyInfo []属性= generetedType.GetProperties();            INT propertiesCounter = 0;            //循环,我们将分配给该属性的值
            的foreach(在xmlDoc.SelectSingleNode(XmlNode的节点根)。子节点)
            {
                字符串值= node.InnerText;
                性能[propertiesCounter] .SetValue(generetedObject,价值,NULL);
                propertiesCounter ++;
            }            // Yoopy!回到我们的新genereted对象。
            返回generetedObject;
        }


解决方案

这是因为RuleModel.Create(类型类型)在内部调用的Assembly.Load(字符串的AssemblyName),它寻找一个装配在默认搜索路径,例如斌/斌/ assembly.dll /。NET温度路径等我希望它会简单地使用通过类型,但事实并非如此。它会尝试加载它。

请确保调用AssemblyBuilder.Save()方法先保存装配,以便它可以由RuleModel后来被载入。

请参阅我的previous答案<一个href=\"http://stackoverflow.com/questions/34911152/how-to-use-a-dinamically-generated-object-as-the-data-source-of-$c$ceffects-gene/34958616#34958616\">How使用dinamically生成的对象为codeEffects发生器的数据源

I am trying to use a dinamycally generated type as the source of one business rules editor called codeeffects (www.codeeffects.com), however I am getting this exception

Could not find or load assembly "tmpAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null". Error message: Could not load file or assembly 'tmpAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. (Error S103)

The index action in the controller is:

[HttpGet]
        public ActionResult Index()
        {
            IMyInterface myObject = (IMyInterface)ObjectBuilder.CreateOurNewObject();
            Type t = myObject.GetType();
            ViewBag.Rule = RuleModel.Create(t);
            return View();
        }

and the method that creates the new object is.

public static object CreateOurNewObject()
        {
            string _xml = "<root>" +
                "<column name=\"Name\">Miron</column>" +
                "<column name=\"LastName\">Abramson</column>" +
                "<column name=\"Blog\">www.blog.mironabramson.com</column>" +
                "</root>";

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(_xml);

            // create a dynamic assembly and module
            AssemblyName assemblyName = new AssemblyName();
            assemblyName.Name = "tmpAssembly";
            System.Reflection.Emit.AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder module = assemblyBuilder.DefineDynamicModule("tmpModule");

            // create a new type builder
            TypeBuilder typeBuilder = module.DefineType("BindableRowCellCollection", TypeAttributes.Public | TypeAttributes.Class);

            typeBuilder.AddInterfaceImplementation(typeof(IMyInterface));

            // Loop over the attributes that will be used as the properties names in out new type
            foreach (XmlNode node in xmlDoc.SelectSingleNode("root").ChildNodes)
            {
                string propertyName = node.Attributes["name"].Value;

                // Generate a private field
                FieldBuilder field = typeBuilder.DefineField("_" + propertyName, typeof(string), FieldAttributes.Private);
                // Generate a public property
                PropertyBuilder property =
                    typeBuilder.DefineProperty(propertyName,
                                     PropertyAttributes.None,
                                     typeof(string),
                                     new Type[] { typeof(string) });

                // The property set and property get methods require a special set of attributes:

                MethodAttributes GetSetAttr =
                    MethodAttributes.Public |
                    MethodAttributes.HideBySig;

                // Define the "get" accessor method for current private field.
                MethodBuilder currGetPropMthdBldr =
                    typeBuilder.DefineMethod("get_value",
                                               GetSetAttr,
                                               typeof(string),
                                               Type.EmptyTypes);

                // Intermediate Language stuff...
                ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
                currGetIL.Emit(OpCodes.Ldarg_0);
                currGetIL.Emit(OpCodes.Ldfld, field);
                currGetIL.Emit(OpCodes.Ret);

                // Define the "set" accessor method for current private field.
                MethodBuilder currSetPropMthdBldr =
                    typeBuilder.DefineMethod("set_value",
                                               GetSetAttr,
                                               null,
                                               new Type[] { typeof(string) });

                // Again some Intermediate Language stuff...
                ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
                currSetIL.Emit(OpCodes.Ldarg_0);
                currSetIL.Emit(OpCodes.Ldarg_1);
                currSetIL.Emit(OpCodes.Stfld, field);
                currSetIL.Emit(OpCodes.Ret);

                // Last, we must map the two methods created above to our PropertyBuilder to
                // their corresponding behaviors, "get" and "set" respectively.
                property.SetGetMethod(currGetPropMthdBldr);
                property.SetSetMethod(currSetPropMthdBldr);
            }

            // Generate our type
            Type generetedType = typeBuilder.CreateType();

            // Now we have our type. Let's create an instance from it:
            object generetedObject = Activator.CreateInstance(generetedType);

            // Loop over all the generated properties, and assign the values from our XML:
            PropertyInfo[] properties = generetedType.GetProperties();

            int propertiesCounter = 0;

            // Loop over the values that we will assign to the properties
            foreach (XmlNode node in xmlDoc.SelectSingleNode("root").ChildNodes)
            {
                string value = node.InnerText;
                properties[propertiesCounter].SetValue(generetedObject, value, null);
                propertiesCounter++;
            }

            //Yoopy ! Return our new genereted object.
            return generetedObject;
        }
解决方案

This happens because the RuleModel.Create(Type type) internally calls the Assembly.Load(string assemblyName) which looks for an assembly in a default search path, e.g. bin/, bin/assembly.dll/, .net temp paths, etc. I wish it'd simply use the passed type, but it doesn't. It tries to load it.

Make sure to call the AssemblyBuilder.Save() method to save the assembly first so that it can be loaded by the RuleModel later.

See my previous answer in How to use a dinamically generated object as the data source of CodeEffects generator

这篇关于无法找到或装载组件&QUOT; tmpAssembly,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:32