反射&安培;应用设计 [英] Reflection & Application Design

查看:142
本文介绍了反射&安培;应用设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我描述我目前拥有的问题和解决方案,也许你可以帮助见识一下为什么这是一个糟糕的主意(假设它是)什么我可以做,使之更好的系统。

Let me describe the problem and the solution i currently have and maybe you can help enlighten me on why it's a bad idea (assuming it is) and what i can do to make it a better system.

现在我有解析文件和RIP他们CSV或其他格式600出挑。在松土都实现一个共同的接口和基类。而此时取决于工作具体开膛手是使用反射所谓的配置的作业排队的那一刻。

right now i have 600 "rippers" that parse files and rip them to csv or other formats. the rippers all implement a common interface and base class. At the moment when a job is queued up depending on the configuration of that job a specific ripper is called using reflection.

            foreach (var stream in streams)
            {
                try
                {
                    // load it
                    Assembly asm = Assembly.LoadFile(Path.Combine(stream.RipperPath, stream.RipperFileName), new Evidence());
                    if (asm == null)
                    {
                        MessageBox.Show(String.Format("Invalid Interface ripper loaded.\n{0}", Path.Combine(stream.RipperPath, stream.RipperFileName)));
                        return;
                    }

                    foreach (Type objType in asm.GetTypes())
                    {
                        if (!objType.IsPublic)
                            continue;

                        // make sure the type isn't Abstract
                        if (((objType.Attributes & TypeAttributes.Abstract) == TypeAttributes.Abstract))
                            continue;

                        // IRipper is the interface that all of the Rippers must implement to be loaded
                        Type objInterface = objType.GetInterface("IRipper", true);
                        if (objInterface == null)
                            continue;

                        try
                        {
                            var iri = (IRipper)Activator.CreateInstance(objType);

                            // Rippers must register with their hosts
                            iri.Host = this;
                            iri.OnStart += RipperStart;
                            iri.OnComplete += RipperComplete;
                            iri.OnProgressChanged += RipperStatusUpdate;
                            iri.dataStream = stream;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("Error loading interface: {0}\n{1}", Path.Combine(stream.RipperPath, stream.RipperFileName), ex.Message));
                }
            }



所有剪辑都实现一个调用的函数(RIP) 该接口他们给合约

all rippers have implement a function called "Rip()" which the interface contracts them to.

使用当前的代码,我可以看到的唯一的问题是,加载600组件(因为在需要的时候他们已经加载的)这将开始得到他们是否已被使用后,他们不会卸载有点慢。

using the current code the only problem i can see is that after loading 600 assemblies (since they're loaded when they are needed) it's going to start getting a little slow if they are not unloaded after they have been used.

您有什么建议?

推荐答案

你实际上有600种不同的松土,或600流和出挑的一些规模较小的号码是多少?你真的需要每个开膛手在它自己组装?如果你能一堆松土在一起,你可以有更少的组件。如果您的流信息中包含的类型名称,以及可执行文件,你可以有多个松土,你不会需要去翻翻组装为他们 - 只需拨打 Assembly.GetType

Do you actually have 600 different rippers, or 600 streams and some smaller number of rippers? Do you really need each ripper to be in its own assembly? If you could bunch the rippers together you could have far fewer assemblies. If your stream details contained the type name as well as the executable, you could have multiple rippers and you wouldn't need to go looking through the assembly for them - just call Assembly.GetType.

我不知道有多少开销实在是每个装配,你要知道 - 我期望600组件占用的内存相当,但不是影响性能远远超出了

I don't know how much overhead there really is per assembly, mind you - I'd expect 600 assemblies to take up a fair amount of memory, but not affect performance much beyond that.

总的做法似乎是合理的,虽然你可能也想看看的托管扩展性框架(MEF) - 这可能是矫枉过正的情况,但值得一试。我会做的另一件事是因素了从处理流码得到一个松土:)

The overall approach seems reasonable, although you might also want to look at the Managed Extensibility Framework (MEF) - it may be overkill for your case, but worth a look. The other thing I'd do is factor out the "getting a ripper" from the "processing a stream" code :)

这篇关于反射&安培;应用设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆