1

I tried to use this code in a NetStandard 1.3 class library:

    foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
    .Where(mytype => mytype .GetInterfaces().Contains(typeof(myInterface)))) {
        //do stuff
     }

but a compilation error is raised:

    Error   CS0117  'Assembly' does not contain a definition for 'GetExecutingAssembly'

I reached What is the equivalent of Assembly.GetEntryAssembly() in .NET Core?, but it can't help.

I know that this service may be available in NetStandard 2, but until it's released(it's now in preview):

What is the modification to be done to the above code to support NetStandard 1.3

Update:

The question isn't a duplicate, i mentioned this link in my question and said it doesn't help.

Based on the comment of @ mjwills, it's probably worth adding that it's best to avoid using netstandard 1.5 or 1.6, as they won't be compatible with netstandard 2.0 https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/#div-comment-136675

C# library for .NET Core and .NET Framework

I ask for an equivalent code (not necessary using GetExecutingAssembly):

How I get all classes that implement an interface in NetStandard 1.1 up to 1.4 ?

9
  • 1
    Possible duplicate of What is the equivalent of Assembly.GetEntryAssembly() in .NET Core? Commented Jul 5, 2017 at 11:06
  • I mentioned this link in my question , but it can't help. Commented Jul 5, 2017 at 11:08
  • The answer there states Assembly.GetEntryAssembly() is available in .NET Standard 1.5, but not in versions 1.0 through 1.4. You need to use a newer version. Commented Jul 5, 2017 at 11:09
  • 1
    t's probably worth adding that it's best to avoid using netstandard 1.5 or 1.6, as they won't be compatible with netstandard 2.0 blogs.msdn.microsoft.com/dotnet/2016/09/26/… Commented Jul 5, 2017 at 11:12
  • 1
    For your update, they changed their minds about the compatability, netstandard 1.5 and 1.6 is compatable with 2.0, from this page ".NET Standard 2.0 will be a strict superset of .NET Standard 1.6. In other words, no breaking changes will happen between .NET Standard 2.0 and 1.x." Commented Jul 5, 2017 at 14:36

1 Answer 1

3

The following code is running in NetStandard1.3 and get all classes that implement interface without compilation error:

            Assembly asm = typeof(ICustomeAttribute).GetTypeInfo().Assembly; //thanks to @mjwills  for his helpfull comment

            var types = asm.DefinedTypes.Where(x=>x.ImplementedInterfaces.Contains(typeof(ICustomeAttribute)));
            foreach (var type in types)
            {
                //do stuff
                Console.WriteLine("class name: {0} - {1}",type.Name,type.FullName);
            }

Remarks

Excerpt from: Assembly.DefinedTypes Property

The DefinedTypes property is comparable to the Assembly.GetTypes method, except that the DefinedTypes property returns a collection of TypeInfo objects, and the Assembly.GetTypes method returns an array of Type objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.