के बाद से गुण वास्तव में सिर्फ तरीकों आप ऐसा करते हैं और get_ साफ कर सकते हैं कर रहे हैं दिखाई:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
var x = p.Something;
Console.ReadLine();
}
public string Something
{
get
{
return MethodBase.GetCurrentMethod().Name;
}
}
}
आप प्रदर्शन प्रोफ़ाइल यदि आप MethodBase.GetCurrentMethod() खोजना चाहिए मील StackFrame की तुलना में तेजी है। .NET 1.1 में आपको रिलीज मोड में स्टैकफ्रेम के साथ भी समस्याएं होंगी (स्मृति से मुझे लगता है कि मुझे लगता है कि यह 3x तेज था)।
यह कहा गया कि मुझे यकीन है कि प्रदर्शन समस्या बहुत अधिक समस्या का कारण नहीं बनती है- हालांकि स्टैकफ्रेम स्लोनेस पर एक रोचक चर्चा found here हो सकती है।
मुझे लगता है कि यदि आप प्रदर्शन के बारे में चिंतित हैं तो एक और विकल्प एक विजुअल स्टूडियो इंटेलिसेंस कोड स्निपेट बनाना होगा जो आपके लिए संपत्ति बनाता है और एक स्ट्रिंग भी बनाता है जो संपत्ति के नाम से मेल खाता है।
[CrmAttribute("firstname")]
public string FirstName
{
get { return GetPropValue<string>(MethodBase.GetCurrentMethod().Name); }
set { SetPropValue(MethodBase.GetCurrentMethod().Name, value); }
}
// this is in a base class, skipped that bit for clairty
public T GetPropValue<T>(string propName)
{
propName = propName.Replace("get_", "").Replace("set_", "");
string attributeName = GetCrmAttributeName(propName);
return GetAttributeValue<T>(attributeName);
}
public void SetPropValue(string propName, object value)
{
propName = propName.Replace("get_", "").Replace("set_", "");
string attributeName = GetCrmAttributeName(propName);
SetAttributeValue(attributeName, value);
}
private static Dictionary<string, string> PropToAttributeMap = new Dictionary<string, string>();
private string GetCrmAttributeName(string propertyName)
{
// keyName for our propertyName to (static) CrmAttributeName cache
string keyName = this.GetType().Name + propertyName;
// have we already done this mapping?
if (!PropToAttributeMap.ContainsKey(keyName))
{
Type t = this.GetType();
PropertyInfo info = t.GetProperty(propertyName);
if (info == null)
{
throw new Exception("Cannot find a propety called " + propertyName);
}
object[] attrs = info.GetCustomAttributes(false);
foreach (object o in attrs)
{
CrmAttributeAttribute attr = o as CrmAttributeAttribute ;
if (attr != null)
{
// found it. Save the mapping for next time.
PropToAttributeMap[keyName] = attr.AttributeName;
return attr.AttributeName;
}
}
throw new Exception("Missing MemberOf attribute for " + info.Name + "." + propertyName + ". Could not auto-access value");
}
// return the existing mapping
string result = PropToAttributeMap[keyName];
return result;
}
वहाँ एक कस्टम विशेषता वर्ग CrmAttributeAttribute बुलाया भी है:
+1 मैं भी इस बारे में सोचा जब System.Configuration.ApplicationSettingsBase –