我試圖通過使用部分類以以下方式訪問字典。這樣做的最終目的是公開這本字典,并允許不同的方法直接從/向它讀/寫。
FilenameABC.cs有這個代碼,到目前為止對我來說似乎沒問題:
namespace Namespace1
{
partial class ClassA
{
public class ExampleDictionary
{
private Dictionary<int, string> _dict;
public ExampleDictionary()
{
_dict = new Dictionary<int, string>();
}
}
}
}
FilenameDEF.cs有這個代碼,這就是我遇到困難的地方。我不確定為什么.Add方法不起作用。在第一個中,我試圖在創建 ExampleDictionary 類的實體后立即使用它,這似乎非常違反直覺(當然,考慮到我目前的知識水平)。在第二個中,我試圖從方法內部添加元素,即
namespace Namespace1
{
partial class ClassA
{
ExampleDictionary instanceOfDict = new ExampleDictionary();
instanceOfDict.Add(1, "Test1"); // This does not work
public static void FunctionA()
{
for (int i = 0; i < 10; i )
{
string text = $"Test{i}";
instanceOfDict.Add(i, text); // This does not work as well
}
}
}
}
請問有人可以幫忙嗎?
uj5u.com熱心網友回復:
靜態方法FunctionA無法訪問 的實體成員ClassA。
要么更改FunctionA為非靜態,要么將字典欄位設為靜態。
uj5u.com熱心網友回復:
我仍然不能完全確定你想要做什么。但我會盡力向你展示一些東西。
我正在接受你寫的這句話:“這樣做的最終目的是公開這本字典,并允許不同的方法直接從/向它讀/寫。” 作為我的向導
部分類問題
正如我在評論中提到的:沒有偏類這樣的東西。partial 關鍵字只是表示一個類可以在多個檔案中定義。類的定義規則保持不變,只是部分類成員可能定義在一個檔案中,而其他部分則在其他檔案中。
您在這里遇到的問題與關鍵字無關partial。
你的兩個// 這不起作用注意
第一個看起來像這樣:
partial class ClassA
{
ExampleDictionary instanceOfDict = new ExampleDictionary();
instanceOfDict.Add(1, "Test1"); // This does not work
//other code
}
第一行宣告了ClassAnamed 的私有成員欄位instanceOfDict并初始化它。這就是它編譯的原因。
第二行嘗試將可運行代碼(函式呼叫)直接放在類的頂級范圍內。你不能那樣做。您可以在類的頂級范圍內放置一堆東西(例如,欄位、屬性、方法、委托、列舉、內部類),但不能將代碼放在那里。
你的第二個問題是這樣的:
public static void FunctionA()
{
for (int i = 0; i < 10; i )
{
string text = $"Test{i}";
instanceOfDict.Add(i, text); // This does not work as well
}
}
FunctionA 是一種static方法。 instanceOfDict是類的私有欄位,未宣告為靜態。您不能從靜態方法中參考實體到實體級成員(方法、屬性、欄位等)。仔細閱讀static關鍵字。
創建一個選擇性公開字典的類
因為你想在一個類中做一些字典作業,但把它暴露給外面,我全力以赴,讓我的類成為通用的,<TKey, TValue>以便它可以與任何型別的字典一起使用。
我還讓它實作了集合初始化模式,這樣你就可以像字典一樣初始化你的類的實體。
要使用集合初始化模式,您的類必須執行以下操作:
實作
IEnumerableorIEnumerable<T>(T可以是任何型別,而不僅僅是您正在收集的型別 -NotImplementedException如果您不在乎,您甚至可以拋出一個)包括一個看起來像
public void Add (SomeType atLeastOneParameter). 如果需要,您可以有多個多載Add。
所以,我從這個開始(注意它實作了IEnumerable<T>:
public partial class TestExposedDictionary<TKey, TValue> : IEnumerable<TValue>
{
private readonly Dictionary<TKey, TValue> _theDictionary = new Dictionary<TKey, TValue>();
public void Add(TKey key, TValue value)
{
_theDictionary.Add(key, value);
}
public IEnumerator<TValue> GetEnumerator()
{
foreach (var kvp in _theDictionary)
{
yield return kvp.Value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
這足以讓一個類看起來像一個字典(你可能想添加一些方法來獲取資料等。但是,這取決于你(你可能想要一個索引器(on TKey)和一個TryGetValue.
But, let's add some more random functionality to the class (put this at the top level scope of the class):
public int Count => _theDictionary.Count;
public IEnumerable<KeyValuePair<TKey, TValue>> GetKeyValuePairs()
{
foreach (var kvp in _theDictionary)
{
yield return kvp;
}
}
That will allow you to see how many things are in your dictionary and allow you to iterate over the items (the KeyValuePairs) that the Dictionary collections. Feel free to add more features.
Finally, to test this...
public static void Test()
{
var testDict = new TestExposedDictionary<int, string> {
{1, "one" },
{5, "five" },
{8, "eight" },
{10, "ten" },
{105, "hundredFive" },
};
// I can call Add in the normal way as well:
testDict.Add(300, "threeHundred");
Console.WriteLine($"TestDict Count: {testDict.Count}");
foreach (string s in testDict)
{
Console.WriteLine(s);
}
}
When I run this code, I get:
TestDict Count: 6
one
five
eight
ten
hundredFive
threeHundred
Splitting this using Partial
I don't know why you want to use partial class. Normally this is done to have two chunks of code that are edited/maintained by different personas (the typical reason is for Visual Studio's templating/wizarding mechanism to maintain one side of the partial class and the programmer the other.
規則非常簡單,您只需public partial class MyClass在兩個檔案中宣告即可。這兩個檔案必須在同一個專案中(即編譯到同一個Assembly 中)并且類必須在同一個命名空間中。一旦你這樣做了,只需在兩個partial class宣告中拆分(頂級范圍)點點滴滴。
在這種情況下,我可能會這樣拆分:
- 一方面,我會放置實作宣告的兩個函式
IEnumerator<TValue>:public IEnumerator<TValue> GetEnumerator()IEnumerator IEnumerable.GetEnumerator()
- 另一方面,我會放其他所有東西:
- 字典宣告和初始化
- 另一個列舉器:
public IEnumerable<KeyValuePair<TKey, TValue>> GetKeyValuePairs() public int Count => _theDictionary.Count;public void Add(TKey key, TValue value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/363743.html
上一篇:使用constchar來組織字串
