Determining if an assembly is part of the .NET fra

2019-03-18 02:51发布

How can I tell from the assembly name, or assembly class (or others like it), whether an assembly is part of the .NET framework (that is, System.windows.Forms)?

So far I've considered the PublicKeyToken, and CodeBase properties, but these are not always the same for the whole framework.

The reason I want this information is to get a list of assemblies that my EXE file is using that need to be on client machines, so I can package the correct files in a setup file without using the Visual Studio setup system. The problem is, I don't want to pick up any .NET framework assemblies, and I want it to be an automatic process that is easy to roll out whenever a major update is finished.

The ultimate solution would be that there is an IsFramework property... :)

7条回答
聊天终结者
2楼-- · 2019-03-18 03:27

To accomplish this I'm using the Product Name embedded within the assembly through the AssemblyProductAttribute.

var attribute = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0] as AssemblyProductAttribute;
var isFrameworkAssembly = (attribute.Product == "Microsoft® .NET Framework");

I'm using this technique to group assemblies by product under the About screen of the application and it seems to work just fine for me.

查看更多
男人必须洒脱
3楼-- · 2019-03-18 03:32

You could use reflection to look at the publisher of the assembly, and coordinate that with the assembly's path. If you find an assembly whose publisher is Microsoft, and which exists somewhere below C:\Windows\Microsoft.NET\Framework it's a safe bet it's part of the runtime.

On second thought, the publisher may not even be necessary. Anything under that path should be part of the runtime (barring a misbehaving application that's diddling where it shouldn't be).

查看更多
4楼-- · 2019-03-18 03:35

I have had to deal with the exact same issue. Unfortunately, all answers given so far are insufficient to safely determine if an assembly is part of the .NET Framework.

Microsoft puts a class named FXAssembly into the global namespace of each framework assembly with a const string indicating the version:

.class private abstract auto ansi sealed beforefieldinit FXAssembly
    extends [mscorlib]System.Object
{
    .field assembly static literal string Version = string('2.0.0.0')

}

Use this "marker" to check if an assembly is a framework assembly. Checking the public key too won't hurt either.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-03-18 03:36

If you know that none of your DLLs will be in the GAC you could check whether each assembly is in the GAC or not. If it is, don't copy it. If it isn't, then do copy it. There is a property on the Assembly class called GlobalAssemblyCache. This would obviously work better in some situations than in others.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-03-18 03:39

I suspect that the method both most reliable and most general is going to be the PublicKeyToken. Yes, there's more than one, but it's going to be a finite list and one that doesn't change very often.

For that matter, you could just have a whitelist of assembly names -- that list, too, will be both finite and static between versions of the framework.

查看更多
闹够了就滚
7楼-- · 2019-03-18 03:50

When you install Visual Studio, you get the reference assemblies in various subfolders of the form C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\{FrameworkName}\{FrameworkVersion} - the most interesting thing could be the RedistList\FrameworkList.xml file that contains a list of all assembly names that were shipped with the given framework version.

E.g. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\RedistList\FrameworkList.xml seems to contain a list of all of .NET 4.0's Framework assemblies.

You could easily use these files to establish static white lists of assemblies.

查看更多
登录 后发表回答