Assembly.GetExecutingAssembly(... [英] Assembly.GetExecutingAssembly(...

查看:136
本文介绍了Assembly.GetExecutingAssembly(...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是VB版本,下面是C#版本. C#版本有效.我使用(在VB版本中)Dim myStream As Stream = ...,并且我使用GetEntryAssembly()无济于事.仍然获得相同的MessageBox来捕获抛出的Null异常.

This is the VB version and below is the C# version. C# version works. I used (in the VB version) Dim myStream As Stream = ... and I used GetEntryAssembly() to no avail. Still get the same MessageBox to trap the Null exception that gets thrown.

Private Overloads Sub Refresh(tag As String)
       Dim profile As New Profile(tag)
       Dim list As List(Of Hero) = profile.HeroList

       Me.panelHeroList.Controls.Clear()
       Dim index As Integer = 0

       For Each hero As Hero In list

           btn.Location = New Point(2 + index * 110, 2)
           btn.Size = New Size(108, 128)

           btn.Text = [String].Format("{0} {1}( {2} )", list(index).Level, list(index).Name, list(index).ParagonLevel)
           btn.ImageAlign = ContentAlignment.MiddleCenter

           btn.TextAlign = ContentAlignment.BottomCenter
           btn.Tag = hero

           Dim myStream As Stream = Assembly.GetEntryAssembly().GetManifestResourceStream(hero.HeadPicture)
           If myStream Is Nothing Then
               MessageBox.Show("Image cannot be opened...Sorry", "System Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
               Exit Sub
           Else
               Dim bmp As New Bitmap(myStream)
               btn.BackgroundImage = bmp
               btn.BackgroundImageLayout = ImageLayout.None
           End If

           AddHandler btn.Click, AddressOf btn_Click

           panelHeroList.Controls.Add(btn)

           System.Math.Max(System.Threading.Interlocked.Increment(index), index - 1)
       Next

   End Sub




它可以在C#中正常工作,并且不,我不会切换到C#.




It works in C# just fine, and NO I''m not switching to C#.

private void Refresh(string tag)
        {
            Profile profile = new Profile(tag);
            List<Hero> list = profile.HeroList;

            this.panelHeroList.Controls.Clear();
            int index = 0;
            foreach (Hero hero in list)
            {
                Button btn = new Button();
                btn.Location = new Point(2 + index * 110, 2);
                btn.Size = new Size(108, 128);

                btn.Text = String.Format("{0} {1}( {2} )", list[index].Level, list[index].Name, list[index].ParagonLevel);
                btn.ImageAlign = ContentAlignment.MiddleCenter;

                btn.TextAlign = ContentAlignment.BottomCenter;
                btn.Tag = hero;


                using (Stream myStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(hero.HeadPicture))
                {
                    Bitmap bmp = new Bitmap(myStream);
                    btn.BackgroundImage = bmp;
                    btn.BackgroundImageLayout = ImageLayout.None;
                }

                btn.Click += new EventHandler(btnHero_Click);

                panelHeroList.Controls.Add(btn);

                index++;
            }


        }

推荐答案

不幸的是,我看不到已发布的代码与C#代码之间的区别,因为您没有显示C#代码.差异更可能是GetManifestResourceStream的参数.我还可以怀疑执行程序集或资源是否不同.顺便说一句,此代码对调用位置很敏感,因为可以在不同的程序集中调用它,这显然会使它不起作用.我宁愿建议尽可能使用GetEntryAssembly并将资源放在每个应用程序的入口程序集中.是的,我知道这并非100%适用于所有情况.

现在,它是图像文件吗?一些已知的类型?很有可能,您根本不需要使用此资源流.同时,您也很可能需要它.例如,您将应用程序图标放在清单资源中(否则Shell不会看到它),然后您需要重用与窗口或表单图标相同的图像.然后,您确实需要阅读清单资源.

但是,让我们暂时假设情况并非如此.如果是这样,使用.res资源会更好.最好将图像添加为添加现有文件".该文件将被添加到项目中(它也将被复制到项目文件树中,因此请确保您不创建重复副本)并在资源文件中引用它.如果文件是* .png(支持许多类型),则将在生成并放置为资源文件的子节点的文件中找到类型为Bitmap的自动生成的静态属性.您只需要使用此静态属性即可:它始终可以使用,初始化等.

抱歉,答案可能还不完整,但是,正如我上面所说,仍然缺少一些信息.

—SA
Unfortunately, I cannot see what is the difference between posted code and the C# code, because you did not show the C# code. It''s more likely that the difference is in the arguments of GetManifestResourceStream. I can also suspect that executing assemblies are different, or the resources. By the way, this code is sensitive to the place where it is called, because it is can be called in a different assembly, which would make in not working, apparently. I would rather advise to use GetEntryAssembly whenever possible and put resource in the entry assembly of each application. Yes, I know that this is not 100% suitable for all cases.

Now, is it an image file? Of some known type? Chances are, you don''t need to use this resource stream at all. At the same time, it''s also likely you really need it. For example, you put the application icon in the manifest resource (otherwise the Shell would not see it), and then you would need to reuse the same image as the window or form icon. Then you really would need to read the manifest resource.

However, let''s assume for a minute that this is not the case, by any chance. If so, you would be much better off with .res resources. It''s the best to add an image as "Add existing file". The file will be added to the project (it will be also copied in the project file tree, so make sure you don''t create a double copy) and reference it in the resource file. If the file is, say *.png (many types are supported), you will find the auto-generated static property of the type Bitmap in the file generated and placed as a child node of the resource file. All would you need is just using this static property: it''s always ready to use, initialized, etc.

Sorry that the answer is probably incomplete, but, as I said above, some information is still missing.

—SA


这篇关于Assembly.GetExecutingAssembly(...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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