可访问性不一致:参数类型''比c#中的方法更难访问 [英] Inconsistent accessibility: parameter type '' is less accessible than method in c#

查看:64
本文介绍了可访问性不一致:参数类型''比c#中的方法更难访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i在这个服务方法中创建了wcf服务我把类称为参数。

这里我收到错误不一致的可访问性:参数类型' '比方法更难接触



代码如下所示



Hi all,
i created wcf services in this service method i called class as a perameter.
here iam getting error "Inconsistent accessibility: parameter type '' is less accessible than method"

the code is like below

namespace demo.TransferObjects
{
    public class Severity
    {
        public string LayerName { get; set; }
        public float AttValue { get; set; }
        public float LimitValue { get; set; }
        public float ExpCost { get; set; }
        public float AttachProb { get; set; }
        public float LossProb { get; set; }
        public float ShareLayer { get; set; }
        public float Premium { get; set; }
    }
}





服务声明我将严重性类称为参数





the service declaration i called the severity class as a perameter

[ServiceContract]
    public interface IDemoService
    {
        [OperationContract]
        [WebInvoke(
            Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            UriTemplate = "/SeverityAnalysis")]
        SeverityResponse SeverityAnalysis(Severity input);

  }



实施方法我也遇到同样的问题。

下面的实施方法




the implementation method also i am getting same issue.
the implemenation method like below

public class DemoService : IDemoService
    {

        public SeverityResponse SeverityAnalysis(Severity input)
        {
            return ServiceHelper.InvokeList<SeverityResponse, Severity>(() =>
            {
                // Calculate severity based on input values
                return new Severity();

            });
        }

    }
}





请将任何信息分享给我。



please share any information to me.

推荐答案

您没有提供问题的完整信息,没有显示错误消息所指的确切行,并且显然没有显示错误消息的定义有问题的参数类型。我知道,因为你展示的所有类型都是 public ,所以他们不可能做得更容易。您很可能会让更多成员 public 确实需要它。有问题的类型可能是也可能不是 SeverityResponse ;我完全不确定,因为你没有提供足够的信息。



下次,请确保提供全面的信息。某些错误消息引用的行应如此评论,并且应在问题文本中引用此类注释。应显示所有相关代码,但不应显示项目的真实代码,这可能太大了。您需要通过极端简化项目代码来开发最小项目,以使代码适合在您的问题中显示。在这种情况下,它将需要大约10行代码。



在这种情况下,您不需要代码,但您必须了解原理。这很简单,你应该通过逻辑思维来理解它。可能有很多类似的案例;我将尝试解释 public 案例的原则。



假设您将某些方法声明为公共。这意味着所有代码都应该可以访问,包括另一个程序集中的代码。也就是说,声明方法也应声明 public ,否则该方法在另一个程序集中仍然是不可接受的。现在,假设此方法具有参数。事实上,有问题的参数无关紧要是泛型参数,或者只是方法参数。想象一下,这是在与 internal 相同的程序集中声明的类或结构,顺便说一下,当您省略类型访问说明符时,保留对默认。现在,想象当您在另一个程序集中实际使用该方法时会发生什么。尽管 public ,但由于其中一个参数无法访问,因此无法使用。因此,您还必须使参数类型 public



在选择访问权限时对其他情况应用类似的注意事项符。从学习它们的意义和目的开始。



-SA
You did not provide full information in the problem, did not show exact line where the error message refers to, and, quite apparently, did not show the definition of the problematic parameter type. I know that because all types you shown are public, so they cannot possibly done more accessible. It's very likely that you made more members public that it is really required. The problematic type may or may not be SeverityResponse; I don't know exactly because you did not provide enough information.

Next time, make sure you provide comprehensive information. The lines referred by some error message should be commented as such, and such comments should be referenced in the question text. All the relevant code should be shown, but not the "real" code of your project, which could be too big. You need to develop minimal project through extreme simplification of your project code, to make the code suitable for showing in your question. In this case, it would take about 10 lines of code.

In this case, you don't need the code, but you have to understand the principle. It's so simple that you were supposed to understand it just through logical thinking. There can be many similar cases; I'll try to explain the principle on the case of public.

Suppose you declare some method as public. It means that is should be accessible to all code, including the code in another assembly. That said, the declaring method should also be declared public, otherwise the method would remain unacceptable in another assembly. Now, suppose this method has parameters. In fact, it does not matter of the problematic parameter is the generic parameter, or just the method parameter. Imagine that this is the class or struct declared in the same assembly as internal, which is, by the way, the case when you omit the type access specifier, leave the access to default. Now, imagine what happens when you actually use the method in another assembly. Despite the public, it could not be used because one of the parameters is not accessible. Therefore, you have to make the parameter type also public.

Apply similar considerations for other cases when choosing access specifiers. Start from learning their meaning and purpose.

—SA


这篇关于可访问性不一致:参数类型''比c#中的方法更难访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆