C# 由于保护级别,无法在同一命名空间内使用公共类的构造函数?
我目前正在尝试映射许多通道,因此为了方便起见,我创建了一个类,该类可以包含通道属性和一个字典,它将一个ID映射到一个通道。词典位于一个类上,通道定义位于它们自己的类中,但位于同一名称空间中C# 由于保护级别,无法在同一命名空间内使用公共类的构造函数?,c#,class,constructor,C#,Class,Constructor,我目前正在尝试映射许多通道,因此为了方便起见,我创建了一个类,该类可以包含通道属性和一个字典,它将一个ID映射到一个通道。词典位于一个类上,通道定义位于它们自己的类中,但位于同一名称空间中 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ParentId = System.String; using C
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ParentId = System.String;
using ChildIds = System.Collections.Generic.List<int>;
using ChannelName = System.String;
using CatalogEntry = System.Tuple<System.String, System.Collections.Generic.List<int>, System.String>;
namespace example
{
public class ChannelHandler
{
public ChannelName channelname { get; set; }
public ParentId parentid { get; set; }
public ChildIds list_of_childs { get; set; }
public int id;
ChannelHandler(ChannelName name, int id)
{
this.channelname = name;
this.id = id;
}
ChannelHandler(ChannelName name, int id, ParentId parentid)
{
this.channelname = name;
this.id = id;
this.parentid = parentid;
}
}
public class Class1
{
private Dictionary<int?, ChannelHandler> dictionary = new Dictionary<int?, ChannelHandler>();
public void inser_into_dictionary(string path)
{
string[] separatingChars = { " " };
string[] splittedPath = path.Split(separatingChars, StringSplitOptions.RemoveEmptyEntries);
int parentId = 0;
ChannelName parentName = string.Empty;
foreach (string channel_id in splittedPath)
{
ChannelName name = channel_id.Split('_').ElementAt(0);
string id = channel_id.Split('_').ElementAt(1);
int int_id = 0;
if (int.TryParse(id, out int_id))
{
int_id = int.Parse(id);
}
Tuple<string, int> pair_of_channel_and_id = new Tuple<string, int>(name, int_id);
if (this.dictionary.ContainsKey(int_id))
{
// Key exist, meaning this is a parent.
// Child entry has to be updated.
parentId = int_id;
parentName = name;
}
else
{
if (parentId == 0)
{
ChannelHandler channel = new ChannelHandler(name, int_id); //ChannelHandler.ChannelHandler is inaccesible due to protection level.
this.dictionary.Add(int_id, channel);
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用ParentId=System.String;
使用childId=System.Collections.Generic.List;
使用ChannelName=System.String;
使用CatalogEntry=System.Tuple;
名称空间示例
{
公共类ChannelHandler
{
公共信道名称信道名称{get;set;}
公共ParentId ParentId{get;set;}
public ChildIds list_of_childs{get;set;}
公共int id;
ChannelHandler(ChannelName名称,int id)
{
this.channelname=名称;
this.id=id;
}
ChannelHandler(ChannelName名称、int id、ParentId ParentId)
{
this.channelname=名称;
this.id=id;
this.parentid=parentid;
}
}
公共班级1
{
私有字典=新字典();
将公共void插入字典(字符串路径)
{
字符串[]separatingChars={”“};
string[]splittedPath=path.Split(分隔字符、StringSplitOptions.RemoveEmptyEntries);
int parentId=0;
ChannelName parentName=string.Empty;
foreach(拆分路径中的字符串通道\u id)
{
ChannelName name=频道id.Split(“”“).ElementAt(0);
字符串id=通道id.Split(“”“).ElementAt(1);
int_id=0;
if(int.TryParse(id,out int_id))
{
int_id=int.Parse(id);
}
_通道_和_id的元组对_=新元组(名称,int_id);
if(this.dictionary.ContainsKey(int_id))
{
//键exist,表示这是父项。
//必须更新子条目。
parentId=int_id;
parentName=name;
}
其他的
{
if(parentId==0)
{
ChannelHandler channel=new ChannelHandler(名称,int_id);//ChannelHandler.ChannelHandler由于保护级别而不可访问。
this.dictionary.Add(int\u id,channel);
}
}
}
}
}
}
我似乎无法创建对象通道
,因为由于保护级别的原因,类的构造函数似乎不可访问?但是什么样的保护级别—所有内容都是公共的?它可能看起来像:
public class ChannelHandler
{
public ChannelName ChannelName { get; set; }
public ParentId ParentId { get; set; }
public List<ChildId> ChildIds { get; set; }
public int Id;
public ChannelHandler(ChannelName name, int id)
{
ChannelName = name;
Id = id;
}
public ChannelHandler(ChannelName name, int id, ParentId parentid)
{
ChannelName = name;
Id = id;
ParentId = parentid;
}
公共类ChannelHandler
{
公共信道名称信道名称{get;set;}
公共ParentId ParentId{get;set;}
公共列表子ID{get;set;}
公共int Id;
公共ChannelHandler(ChannelName,int-id)
{
ChannelName=名称;
Id=Id;
}
公共ChannelHandler(ChannelName、int-id、ParentId-ParentId)
{
ChannelName=名称;
Id=Id;
ParentId=ParentId;
}
默认情况下,类的任何元素的访问级别都是private
。您需要将构造函数设置为public或internal
,以便可以从外部访问类本身。如果您阅读本文,它将解释访问修饰符如何影响代码的访问级别
我做的另一个更改是更改您的成员的大小写以满足c#
编码约定。请参见它可能看起来像:
public class ChannelHandler
{
public ChannelName ChannelName { get; set; }
public ParentId ParentId { get; set; }
public List<ChildId> ChildIds { get; set; }
public int Id;
public ChannelHandler(ChannelName name, int id)
{
ChannelName = name;
Id = id;
}
public ChannelHandler(ChannelName name, int id, ParentId parentid)
{
ChannelName = name;
Id = id;
ParentId = parentid;
}
公共类ChannelHandler
{
公共信道名称信道名称{get;set;}
公共ParentId ParentId{get;set;}
公共列表子ID{get;set;}
公共int Id;
公共ChannelHandler(ChannelName,int-id)
{
ChannelName=名称;
Id=Id;
}
公共ChannelHandler(ChannelName、int-id、ParentId-ParentId)
{
ChannelName=名称;
Id=Id;
ParentId=ParentId;
}
默认情况下,类的任何元素的访问级别都是private
。您需要将构造函数设置为public或internal
,以便可以从外部访问类本身。如果您阅读本文,它将解释访问修饰符如何影响代码的访问级别
我所做的另一个更改是更改成员的大小写以满足
c#
编码约定。请参见您将类名声明为public,但这还不够。类定义中其他类可以访问的每个函数,包括构造函数,也必须声明为public。构造函数和方法没有访问修饰符的类中的ds默认为private或internal,具体取决于具体情况。有关详细信息,请访问此链接:
您已将类名声明为public,但这还不够。类定义中可由其他类访问的每个函数(包括构造函数)也必须声明为public。不带访问修饰符的类中的构造函数和方法默认为private或internal,具体取决于circumstances。有关更多信息,请访问此链接:
类
通道处理程序
构造函数不能从类外调用,因为它们是私有的
。当您没有为方法或字段指定访问级别修饰符时,默认情况下,C编译器将假定它是私有的
。只需在构造函数定义之前添加关键字public
:
public class ChannelHandler
{
public ChannelName channelname { get; set; }
public ParentId parentid { get; set; }
public ChildIds list_of_childs { get; set; }
public int id;
public ChannelHandler(ChannelName name, int id)
{
this.channelname = name;
this.id = id;
}
public ChannelHandler(ChannelName name, int id, ParentId parentid)
{
this.channelname = name;
this.id = id;
this.parentid = parentid;
}
}
类
ChannelHandler
construct
internal ChannelHandler(ChannelName name, int id)
{
channelname = name;
this.id = id;
}
internal ChannelHandler(ChannelName name, int id, ParentId parentid)
: this(name, id)
{
this.parentid = parentid;
}