Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:39:36+00:00 2026-06-07T23:39:36+00:00

In my MiddleTier project in a solution I have Customer classes, one of them

  • 0

In my MiddleTier project in a solution I have Customer classes, one of them defined with Attribute1

public class Customer2
{
    public string Name2 { get; set; }

    public int Age2 { get; set; }
}

[MyAttribute1]
public class Customer1
{
    [MyAttribute1(DefaultValue = "Must Enter Name")]
    public string Name { get; set; }

    [MyAttribute1(DefaultValue = "Must Enter Age")]
    public int Age { get; set; }
}

[AttributeUsage(AttributeTargets.All)]
public class MyAttribute1 : Attribute
{
    public string DefaultValue { get; set; }

}

In a separate project, I reference MiddleTier DLL and I want to enumerate through all the classes in this DLL and identify Customer1 class that is associated with Attribute1.

            Assembly assembly =  Assembly.LoadFrom(@"C:\myfolder\MiddleTier\bin\Debug\MiddleTier.dll");

            foreach (Type type in assembly.GetTypes())
            {

                var attribs = type.GetCustomAttributes(typeof(MyAttribute1), false); <--- problem
                if (attribs != null && attribs.Length > 0)
                {
....
                }
            }

I am not getting any attributes back by the GetCustomAttributes call. What am I doing wrong? please help. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:39:38+00:00Added an answer on June 7, 2026 at 11:39 pm

    The problem is that you are actually loading the assembly twice and doing so from two different locations. This results in the assemblies being loaded into two different contexts, which, in turn, results in your types being incompatible. You can easily verify this when you try to run this code (of course, you will need to change the path to the assembly):

    foreach (Type type in Assembly.LoadFrom(@"C:\ClassLibrary1.dll").GetTypes())
    {
         MyAttribute1 attribute = type.GetCustomAttributes(false)
                                      .Cast<MyAttribute1>()
                                      .SingleOrDefault();
        if (attribute != null)
        {
           Console.WriteLine(type.Name);
        }
    
    }
    

    That will result in following exception being thrown:

    [A]ClassLibrary1.MyAttribute1 cannot be cast to
    [B]ClassLibrary1.MyAttribute1. Type A originates from ‘ClassLibrary1,
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ in the context
    ‘LoadFrom’ at location ‘C:\ClassLibrary1.dll’. Type B originates from
    ‘ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’
    in the context ‘Default’ at location ‘C:\Users\Nikola\Documents\Visual
    Studio
    2010\Projects\ConsoleApplication12\ConsoleApplication12\bin\Debug\ClassLibrary1.dll’.

    So, how to solve it?

    You simply need to load the assembly by using Assembly.Load("ClassLibrary1"). That will make sure that you are working within the single context and your original code will work.

    Check out this blogpost which treats upon exactly the same issue you are having. It also may be helpful to read something about load contexts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a potential project where I would be working on developing the Data
I have been working on a web project that stores locations for users. The
Folks, I am having a problem with a c#, multi-project solution in Visual Studio
Folks, I have an ASP.NET project which is pretty n-tier, by namespace, but I
Suppose I get data from a service (that I can't control) as: public class
Given a (current) project that implements a 2-tier architecture with a well-separated on-tier business
I've got an architecture like the following: Data (Class Library that handles our Entity
I have a table in my database called OrderItemType which has about 5 records
I have inherited a conventional three tier web app with ASP.net 2.0 for the
I have to add e-mail notifications to a client server application. Notifications happen as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.