0

I have searched but failed to know how to check a class is user defined class or c#.net assembly class. Please help me in this point. When I work with assembly I need to know which classes are user define and which are not? I also need to know class attributes type.

Amin Uddin
  • 968
  • 6
  • 15
  • 31
  • ".NET Assembly Class" = ? A class that ships with the .NET framework? A class that ships with a common .NET library (not included in the .NET [BCL/FCL](http://stackoverflow.com/questions/807880/bcl-base-class-library-vs-fcl-framework-class-library))? Not-a-class-I-wrote? – user2864740 Sep 07 '14 at 05:54
  • I need to know a class is user defined or not. – Amin Uddin Sep 07 '14 at 05:55
  • 1
    Defined by *which* user? Every class was created by *someone* (or by an automated request from such). – user2864740 Sep 07 '14 at 05:56
  • Custom class or not defined by .net framework. – Amin Uddin Sep 07 '14 at 05:56
  • 1
    What about a class in (eg.) EF, which if not shipped with the core .NET Framework? – user2864740 Sep 07 '14 at 05:57
  • I think you'd better do this the other way around instead of recognizing .Net framework classes , introduce .Net assemblies to your project (for example by getting their names from the Assembly folder). – Beatles1692 Sep 07 '14 at 06:02
  • Could you explain *why* you need to identify user defined classes? [Maybe there's an easier solution that someone can suggest](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – default Sep 07 '14 at 08:34

3 Answers3

0

Try by pressing F12, and check in class defination. Maybe it might help.

Mufaddal
  • 558
  • 5
  • 24
0

Not very clear what you mean, but if you examine

yourObj.GetType().Assembly.Location

(or .CodeBase) you will get a hint. You will need to match the string against expected locations.

You can also use attributes, for example:

var compAttr = yourObj.GetType().Assembly
    .GetCustomAttribute<AssemblyCompanyAttribute>();
var name = compAttr != null ? compAttr.Company : null;

which needs using System.Refelection;. (In older versions (.NET 4 and earlier) the syntax for reflection is a bit more complicated.)

If you make the "user-defined" code, you can apply a:

[assembly: MadeByMe]

to each of your projects (where public class MadeByMeAttribute : Attribute needs to be written). Then search for that attribute, as it is certain never to be present in BCL assemblies.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
0

You could opt to check the public key token of the assembly to check if it matches that of Microsoft:

Try this extension method:

public static bool IsMicrosoftAssembly(this Assembly assembly)
{
    return assembly.GetName().GetPublicKeyToken()
           .SequenceEqual(typeof(int).Assembly.GetName().GetPublicKeyToken());
}

Use it like this:

var n = typeof(YourType).Assembly.IsMicrosoftAssembly();
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325