الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة مع Dynamic Linking و Reflection

مغلق
بدأه Usama Fayez في 6 يناير 2006 · 0 رد · 366 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1

السلام عليكم و رحمة الله و بركاته

أنا بصمم برنامج و حبيت أعمل فيه Plugin Support بس قابلتني مشكلة

أنا لما جيت أربط Dll واحدة إشتغل عادي بالكود ده

الأول خالص أنا عملت Class Library و عملت فيها Interface سميته EditorInterface.IEditor

و عملت Class Library تاني ( Plugin ) و عملت Class فيه بيعمل Implement لل Interface الأولى طبعا

و في المشروع الأساسي بعد ما عملت Add Reference لل Dll إللي فيها ال Interface و جربت الكود ده

            EditorInterface.IEditor Editor;
            string ClassName = "";
            Assembly asm = Assembly.LoadFrom("D:\\Programming\\Projects\\WebDevelop\\WebDevelop\\bin\\Debug\\Engines\\TextEditor.dll");

            foreach (Type objType in asm.GetTypes())
            {
                if ((objType.GetInterface("EditorInterface.IEditor", true)) != null)
                {
                    ClassName = objType.FullName;
                }
            }


            Editor = (EditorInterface.IEditor)asm.CreateInstance(ClassName);

asm : هو ال Assembly إللي بيمثل ال Plugin إللي بيعمل Implement لل Interface و بيحتوي على ال Class إللي عايز أعمل نسخة منه و TextEditor.dll هي الDll إللي بتحتوي على الكلاس ده

في الكود السابق أنا عملت foreach loop علشان أدور على الكلاس إللي بيعمل Implement لل Interface و في الأخر أعمل نسخة منه بCreateInstance

المهم الكود ده إشتغل عادي بس حبيت أخليه يدور على كل ال Dlls في مجلد خاص بيهم سميته Engines و يدور في كل واحد فيهم عن الكلاس إللي بيعمل Implement للInterface دي بنفس الطريقة إللي فاتت بالكود ده

        // Hashtable of the avaliable file types
        System.Collections.Hashtable FilesTemplates;

            //  Initialize the template Hashtable
            FilesTemplates = new System.Collections.Hashtable();

            //  All the DLLs in the Engines Path
            string[] strDLLs = System.IO.Directory.GetFileSystemEntries(Application.StartupPath + "\\Engines", "*.dll");

            System.Reflection.Assembly objDll;

            List<WebDevelop.Templates.PluginServices.AvaliablePlugin> plugins;

            try
            {
                for (int intIndex = 0; intIndex < strDLLs.Length; intIndex++)
                {
                    objDll = System.Reflection.Assembly.LoadFrom(strDLLs[intIndex]);
                    if (objDll != null)
                    {
                        foreach (Type objType in objDll.GetTypes())
                        {
                            if (objType.IsPublic)
                            {
                                if (!((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))
                                {
                                    if ((objType.GetInterface("EditorInterface.IEditor", true)) != null)
                                    {
                                        EditorInterface.IEditor editor;

                                        // *** Here comes the error
                                        editor = (EditorInterface.IEditor)objDll.CreateInstance(objType.FullName);
                                        FilesTemplates.Add(editor.FileType, editor);
                                    }
                                }
                            }
                        }
                    }
                    
                }
            }
            catch
            {
            }

الكود ده بيدي error غريب و بيقول

Unable to cast object of type 'TextEditor.Editor' to type 'EditorInterface.IEditor'.

طب إذاي يا عم الحاج مش قادر تحول من TextEditor.Editor ل EditorInterface.IEditor

مع إن الكلاس TextEditor.Editor أصلا بيعمل Implement من الإنترفاس EditorInterface.IEditor

ياريت لو حد يعرف ليه يقولي علشان أنا إحترت معاه

هذا الموضوع مغلق.

مواضيع مشابهة