मेरे पास निम्न कोड है जो मुझे बताएगा कि कोड में कहीं और एक निश्चित संपत्ति का उपयोग किया जाता है या नहीं। इसके पीछे विचार यह सत्यापित करना है कि private
सेटर वाली संपत्ति को केवल पढ़ने के लिए बनाया जा सकता है या नहीं।ReSharper मुझे क्यों बताता है कि यह अभिव्यक्ति हमेशा सच है?
यहां कई मिलचेक हैं लेकिन प्रमुख लोग हैं कि कन्स्ट्रक्टर के बाहर संपत्ति के लिए असाइनमेंट का अर्थ है कि यह आग नहीं लगेगा। इसके अलावा, एक स्थैतिक संपत्ति में नैदानिक आग लगाने के लिए केवल एक स्थिर निर्माता में असाइनमेंट हो सकता है। इसी प्रकार, एक इंस्टेंस प्रॉपर्टी के लिए केवल एक इंस्टेंस कन्स्ट्रक्टर की आवश्यकता होती है।
अब, अब तक के अधिकांश परिदृश्यों के लिए जिम्मेदार है, फिर भी रिशेपर मुझे कोड के इस टुकड़े में एक चेतावनी देता है और मैं बस इसके तर्क को समझने के लिए प्रतीत नहीं कर सकता। ऊपर विनिर्देश कोड के इस बिट में अनुवाद किया है:
var isStaticProperty = propertySymbol.IsStatic;
bool hasInstanceUsage = false;
bool hasStaticUsage = false;
foreach (var identifier in outerClass.DescendantNodes().OfType<IdentifierNameSyntax>())
{
var memberSymbol = context.SemanticModel.GetSymbolInfo(identifier);
if (memberSymbol.Symbol.Equals(propertySymbol))
{
var constructor = identifier.Ancestors().OfType<ConstructorDeclarationSyntax>()
.FirstOrDefault();
var isInConstructor = constructor != null;
var isAssignmentExpression = identifier.Ancestors()
.OfType<AssignmentExpressionSyntax>()
.FirstOrDefault() != null;
// Skip anything that isn't a setter
if (!isAssignmentExpression)
{
continue;
}
// if it is a setter but outside the constructor, we don't report any diagnostic
if (!isInConstructor)
{
return;
}
var isStaticConstructor = context.SemanticModel
.GetDeclaredSymbol(constructor).IsStatic;
if (isStaticConstructor && isStaticProperty)
{
hasStaticUsage = true;
}
if (!isStaticConstructor && !isStaticProperty)
{
hasInstanceUsage = true;
}
}
}
// We can't set it to readonly if it's set in both the instance
// and the static constructor
// We need a NAND operation: either it's never set,
// it's set in ctor 1 or it's set in ctor 2
if (!(hasStaticUsage & hasInstanceUsage))
{
context.ReportDiagnostic(Diagnostic.Create(
Rule, property.Identifier.GetLocation(), propertySymbol.Name));
}
चेतावनी जा रहा है
अभिव्यक्ति के साथ हमेशा लाइन
if (!(hasStaticUsage & hasInstanceUsage))
क्यों पर सच
है यह इस चेतावनी को दिखाता है? वंशजों की एक अज्ञात राशि है इसलिए लापता होने की अज्ञात राशि है। प्रत्येक पाश true
, कि (जल्द से जल्द) 2 छोरों के बाद, दोनों मूल्यों true
बन सकता है जिसका मतलब है कि करने के लिए hasStaticUsage
या hasInstanceUsage
सेट कर सकते हैं और अगर हालत विफल करना चाहिए: एक NAND रिटर्न true
, true
, true
, false
।
if (isStaticConstructor && isStaticProperty)
{
hasStaticUsage = true;
}
if (!isStaticConstructor && !isStaticProperty)
{
hasInstanceUsage = true;
}
केवल चर में से एक:
+----------------+------------------+--------+
| hasStaticUsage | hasInstanceUsage | result |
+----------------+------------------+--------+
| false | false | true |
| false | true | true |
| true | false | true |
| true | true | false |
+----------------+------------------+--------+
'isStaticProperty' कहाँ आरंभ नहीं हो जाता? –
'hasInstanceUsage' के समान स्तर पर। मैं इसे पूर्णता के लिए शामिल करूंगा, लेकिन मुझे नहीं लगता कि इसका असर पड़ता है। –
'मैं है निम्नलिखित कोड जो मुझे बता देगा या नहीं, एक निश्चित संपत्ति code' में कहीं प्रयोग किया जाता है - नहीं इस वी.एस. में एक देशी सुविधा है? – Davor