Unable to access a new class added to a class library

自古美人都是妖i 提交于 2020-03-03 02:31:50

问题


I have a class library that I added another class to that no matter what I try it will not be available in the project that I am referencing the library from. I have no problem with the original class I created in this library referencing and using.

I have tried all of the below:

  1. Cleaning the project solution
  2. Save and rebuild both the debug and release
  3. Closing the project and reopening
  4. Steps one through three on the library project I'm tyring to reference

In the project that I want to reference the library from I have tried loading the .dll form the bin/release folded, and the main library project .dll in the obj/release folder. Neater have made a difference because I still cannot get to the new class I added to the library. I am referencing the DotNetOpenAuth_Library.dll from the release folded in the bin.

If this makes a difference I'm using VS 2012 Express for Web that I downloaded last week.

The class I added to my library that has no build errors is:

namespace DotNetOpenAuth_Library
{
    class EmbeddedResourceUrlService : IEmbeddedResourceRetrieval
    {
        private static string pathFormat = "{0}/Resource/GetWebResourceUrl?    assemblyName=    {1}&typeName={2}&resourceName={3}";
        //private static string pathFormat = "{0}/Resource/GetWebResourceUrl";

        public Uri GetWebResourceUrl(Type someTypeInResourceAssembly, string     manifestResourceName)
    {
        if (manifestResourceName.Contains("http"))
        {
            return new Uri(manifestResourceName);
        }
        else
        {
            var assembly = someTypeInResourceAssembly.Assembly;

            // HACK
            string completeUrl = HttpContext.Current.Request.Url.ToString();
            string host = completeUrl.Substring(0,
                completeUrl.IndexOf(HttpContext.Current.Request.Url.AbsolutePath));

            var path = string.Format(pathFormat,
                        host,
                        HttpUtility.UrlEncode(assembly.FullName),
                        HttpUtility.UrlEncode(someTypeInResourceAssembly.ToString()),
                        HttpUtility.UrlEncode(manifestResourceName));

            return new Uri(path);
        }
    }
}

}


回答1:


Put public in front of the class definition. If the class is marked internal1 it can only be accessed by other classes within the same assembly2.

namespace DotNetOpenAuth_Library
{
    public class EmbeddedResourceUrlService : IEmbeddedResourceRetrieval
    {
        //(snip)
    }
}

Here is a MSDN link explaining access modifiers.

1: If you do not put a modifier in front of the class it will default to internal.
2: unless you mark the other assembly a friend assembly




回答2:


It looks to me like the problem is just the lack of an access modifier. By default the c# compiler treats classes as internal. It should work if you change the declaration to

public class EmbeddedResourceUrlService : IEmbeddedResourceRetrieval



回答3:


The class EmbeddedResourceUrlService is private, use public modifier

namespace DotNetOpenAuth_Library
{
    // make class public
    public class EmbeddedResourceUrlService : IEmbeddedResourceRetrieval
    {
        private static string pathFormat = "{0}/Resource/GetWebResourceUrl?    assemblyName=    {1}&typeName={2}&resourceName={3}";
        //private static string pathFormat = "{0}/Resource/GetWebResourceUrl";

        public Uri GetWebResourceUrl(Type someTypeInResourceAssembly, string     manifestResourceName)
    {
        if (manifestResourceName.Contains("http"))
        {
            return new Uri(manifestResourceName);
        }
        else
        {
            var assembly = someTypeInResourceAssembly.Assembly;

            // HACK
            string completeUrl = HttpContext.Current.Request.Url.ToString();
            string host = completeUrl.Substring(0,
                completeUrl.IndexOf(HttpContext.Current.Request.Url.AbsolutePath));

            var path = string.Format(pathFormat,
                        host,
                        HttpUtility.UrlEncode(assembly.FullName),
                        HttpUtility.UrlEncode(someTypeInResourceAssembly.ToString()),
                        HttpUtility.UrlEncode(manifestResourceName));

            return new Uri(path);
        }
    }
}

}

even if then the class does not shows up (has happended to a couple time)

  1. clean solution
  2. delete all bin folder from both project
  3. rebuilt all

and error wont be there



来源:https://stackoverflow.com/questions/14411368/unable-to-access-a-new-class-added-to-a-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!