本文整理汇总了C#中Mono.Cecil.PropertyDefinition类的典型用法代码示例。如果您正苦于以下问题:C# PropertyDefinition类的具体用法?C# PropertyDefinition怎么用?C# PropertyDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PropertyDefinition类属于Mono.Cecil命名空间,在下文中一共展示了PropertyDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzedPropertyOverridesTreeNode
public AnalyzedPropertyOverridesTreeNode(PropertyDefinition analyzedProperty)
{
if (analyzedProperty == null)
throw new ArgumentNullException("analyzedProperty");
this.analyzedProperty = analyzedProperty;
}
示例2: CheckIfGetterCallsVirtualBaseSetter
public bool CheckIfGetterCallsVirtualBaseSetter(PropertyDefinition propertyDefinition)
{
if (propertyDefinition.SetMethod.IsVirtual)
{
var baseType = Resolve(propertyDefinition.DeclaringType.BaseType);
var baseProperty = baseType.Properties.FirstOrDefault(x => x.Name == propertyDefinition.Name);
if (baseProperty != null)
{
if (propertyDefinition.GetMethod != null)
{
var instructions = propertyDefinition.GetMethod.Body.Instructions;
foreach (var instruction in instructions)
{
if (instruction.OpCode == OpCodes.Call
&& instruction.Operand is MethodReference
&& ((MethodReference) instruction.Operand).Resolve() == baseProperty.SetMethod)
{
return true;
}
}
}
}
}
return false;
}
示例3: CecilPropertyDescriptor
public CecilPropertyDescriptor (CecilWidgetLibrary lib, XmlElement elem, Stetic.ItemGroup group, Stetic.ClassDescriptor klass, PropertyDefinition pinfo): base (elem, group, klass)
{
string tname;
if (pinfo != null) {
name = pinfo.Name;
tname = pinfo.PropertyType.FullName;
canWrite = pinfo.SetMethod != null;
}
else {
name = elem.GetAttribute ("name");
tname = elem.GetAttribute ("type");
canWrite = elem.Attributes ["canWrite"] == null;
}
Load (elem);
type = Stetic.Registry.GetType (tname, false);
if (type == null) {
Console.WriteLine ("Could not find type: " + tname);
type = typeof(string);
}
if (type.IsValueType)
initialValue = Activator.CreateInstance (type);
// Consider all properties runtime-properties, since they have been created
// from class properties.
isRuntimeProperty = true;
if (pinfo != null)
SaveCecilXml (elem);
}
示例4: AddBeforeAfterInvokerCall
int AddBeforeAfterInvokerCall(int index, PropertyDefinition property)
{
var beforeVariable = new VariableDefinition(typeSystem.Object);
setMethodBody.Variables.Add(beforeVariable);
var afterVariable = new VariableDefinition(typeSystem.Object);
setMethodBody.Variables.Add(afterVariable);
var getMethod = property.GetMethod.GetGeneric();
index = instructions.Insert(index,
Instruction.Create(OpCodes.Ldarg_0),
CreateCall(getMethod),
Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType),
Instruction.Create(OpCodes.Stloc, afterVariable),
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldstr, property.Name),
Instruction.Create(OpCodes.Ldloc, beforeVariable),
Instruction.Create(OpCodes.Ldloc, afterVariable),
CallEventInvoker()
);
instructions.Prepend(
Instruction.Create(OpCodes.Ldarg_0),
CreateCall(getMethod),
Instruction.Create(OpCodes.Box, property.GetMethod.ReturnType),
Instruction.Create(OpCodes.Stloc, beforeVariable));
return index + 4;
}
示例5: Add
public void Add(PropertyDefinition value)
{
if (!Contains (value))
Attach (value);
List.Add (value);
}
示例6: SignatureForProperty
private static string SignatureForProperty(PropertyDefinition property, bool hyperLinked)
{
var hb = new HtmlBuilder();
if (property.IsStatic())
{
hb.AddKeyword("static");
hb.Add(" ");
}
hb.AddKeyword("public");
hb.Add(" ");
hb.AddTypeReference(property.PropertyType, hyperLinked);
hb.Add(" ");
hb.AddMemberReferenceName(property, false);
hb.Add(" {");
if (property.GetMethod != null)
{
hb.Add(" ");
hb.AddKeyword("get");
hb.Add(";");
}
if (property.SetMethod != null)
{
hb.Add(" ");
hb.AddKeyword("set");
hb.Add(";");
}
hb.Add(" }");
return hb.ToString();
}
示例7: AddCollectionCode
private static void AddCollectionCode(PropertyDefinition property, bool isFirst, Collection<Instruction> ins, VariableDefinition resultVariable, MethodDefinition method, TypeDefinition type)
{
if (isFirst)
{
ins.Add(Instruction.Create(OpCodes.Ldc_I4_0));
ins.Add(Instruction.Create(OpCodes.Stloc, resultVariable));
}
ins.If(
c =>
{
LoadVariable(property, c, type);
},
t =>
{
LoadVariable(property, t, type);
var enumeratorVariable = method.Body.Variables.Add(property.Name + "Enumarator", ReferenceFinder.IEnumerator.TypeReference);
var currentVariable = method.Body.Variables.Add(property.Name + "Current", ReferenceFinder.Object.TypeReference);
GetEnumerator(t, enumeratorVariable);
AddCollectionLoop(resultVariable, t, enumeratorVariable, currentVariable);
},
f => { });
}
示例8: Execute
public static void Execute(PropertyDefinition property_definition, MethodDefinition notify_method, DependencyMap map)
{
// Check if notifications are already call, if so bail out
foreach (var instruction in property_definition.SetMethod.Body.Instructions)
{
if (instruction.OpCode == OpCodes.Call)
{
var method = instruction.Operand as MethodDefinition;
if (method != null && method == notify_method)
{
log.Trace("\t\t\t\t\tBailing out, notification found in property");
return;
}
}
}
// Add notifications
var ret = property_definition.SetMethod.Body.Instructions.Last(i => i.OpCode == OpCodes.Ret);
ILProcessor processor = property_definition.SetMethod.Body.GetILProcessor();
// NotifyPropertyChanged(property)
processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
processor.InsertBefore(ret, processor.Create(OpCodes.Ldstr, property_definition.Name));
processor.InsertBefore(ret, processor.Create(OpCodes.Call, notify_method));
// Add notifications for dependent properties
foreach (var target in map.GetDependenciesFor(property_definition.Name))
{
log.Trace("\t\t\t\t\tAdding dependency " + target);
processor.InsertBefore(ret, processor.Create(OpCodes.Ldarg_0));
processor.InsertBefore(ret, processor.Create(OpCodes.Ldstr, target));
processor.InsertBefore(ret, processor.Create(OpCodes.Call, notify_method));
}
}
示例9: GetSingleField
static FieldDefinition GetSingleField(PropertyDefinition property, Code code, MethodDefinition methodDefinition)
{
if (methodDefinition?.Body == null)
{
return null;
}
FieldReference fieldReference = null;
foreach (var instruction in methodDefinition.Body.Instructions)
{
if (instruction.OpCode.Code == code)
{
//if fieldReference is not null then we are at the second one
if (fieldReference != null)
{
return null;
}
var field = instruction.Operand as FieldReference;
if (field != null)
{
if (field.DeclaringType != property.DeclaringType)
{
continue;
}
if (field.FieldType != property.PropertyType)
{
continue;
}
fieldReference = field;
}
}
}
return fieldReference?.Resolve();
}
示例10: TryGetField
static FieldDefinition TryGetField(TypeDefinition typeDefinition, PropertyDefinition property)
{
var propertyName = property.Name;
var fieldsWithSameType = typeDefinition.Fields.Where(x => x.DeclaringType == typeDefinition).ToList();
foreach (var field in fieldsWithSameType)
{
//AutoProp
if (field.Name == $"<{propertyName}>k__BackingField")
{
return field;
}
}
foreach (var field in fieldsWithSameType)
{
//diffCase
var upperPropertyName = propertyName.ToUpper();
var fieldUpper = field.Name.ToUpper();
if (fieldUpper == upperPropertyName)
{
return field;
}
//underScore
if (fieldUpper == "_" + upperPropertyName)
{
return field;
}
}
return GetSingleField(property);
}
示例11: PropertyReferenceReflectionEmitter
public PropertyReferenceReflectionEmitter(MemberReferenceExpression memberReferenceExpression,
Type target,
MemberInfo member,
ILGenerator ilGenerator,
IOpCodeIndexer instructionsIndexer,
IAstVisitor<ILGenerator, AstNode> visitor,
List<LocalBuilder> locals,
bool isSetter = false)
: base(memberReferenceExpression, target, member, ilGenerator, instructionsIndexer, visitor, locals) {
var propertyInfo = Member as PropertyInfo;
_isSetter = isSetter;
_propertyDefinition = MemberReference.Annotation<Cecil.PropertyDefinition>();
NonPublic = !_propertyDefinition.GetMethod.IsPublic;
Type = _propertyDefinition.PropertyType.GetActualType();
if (isSetter) {
_propertyMethod = propertyInfo.GetSetMethod(NonPublic);
_emitPrivateAction = EmitPrivateStorePropertyReference;
}
else {
_propertyMethod = propertyInfo.GetGetMethod(NonPublic);
_emitPrivateAction = EmitPrivateLoadPropertyReference;
}
}
示例12: AddPropertySetter
public static MethodDefinition AddPropertySetter(
PropertyDefinition property
, MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual
, FieldDefinition backingField = null)
{
if (backingField == null)
{
// TODO: Try and find existing friendly named backingFields first.
backingField = AddPropertyBackingField(property);
}
var methodName = "set_" + property.Name;
var setter = new MethodDefinition(methodName, methodAttributes, property.Module.TypeSystem.Void)
{
IsSetter = true,
Body = { InitLocals = true },
};
setter.Parameters.Add(new ParameterDefinition("value", ParameterAttributes.None, property.PropertyType));
setter.Body.Instructions.Append(
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldarg_1),
Instruction.Create(OpCodes.Stfld, backingField),
Instruction.Create(OpCodes.Ret)
);
property.SetMethod = setter;
property.DeclaringType.Methods.Add(setter);
return setter;
}
示例13: AddPropertyGetter
public static MethodDefinition AddPropertyGetter(
PropertyDefinition property
, MethodAttributes methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot | MethodAttributes.Virtual
, FieldDefinition backingField = null)
{
if (backingField == null)
{
// TODO: Try and find existing friendly named backingFields first.
backingField = AddPropertyBackingField(property);
}
var methodName = "get_" + property.Name;
var getter = new MethodDefinition(methodName, methodAttributes, property.PropertyType)
{
IsGetter = true,
Body = {InitLocals = true},
};
getter.Body.Variables.Add(new VariableDefinition(property.PropertyType));
var returnStart = Instruction.Create(OpCodes.Ldloc_0);
getter.Body.Instructions.Append(
Instruction.Create(OpCodes.Ldarg_0),
Instruction.Create(OpCodes.Ldfld, backingField),
Instruction.Create(OpCodes.Stloc_0),
Instruction.Create(OpCodes.Br_S, returnStart),
returnStart,
Instruction.Create(OpCodes.Ret)
);
property.GetMethod = getter;
property.DeclaringType.Methods.Add(getter);
return getter;
}
示例14: ChangeFieldReferencesToPropertyVisitor
public ChangeFieldReferencesToPropertyVisitor(FieldDefinition field, PropertyDefinition property)
{
if (field == null) throw new ArgumentNullException("field");
if (property == null) throw new ArgumentNullException("property");
_field = field;
_property = property;
}
示例15: InstantiateCollection
private void InstantiateCollection(TypeDefinition typeDef, MethodDefinition[] constructors, PropertyDefinition propDef) {
var constructor = constructors.First();
if (constructors.Length > 1) {
constructor = constructors.SingleOrDefault(s => !s.HasParameters && !s.IsStatic);
if (constructor == null) {
this.Log.Error("Type " + typeDef.FullName + " does not have a parameterless constructor for instantiating collections in");
}
}
var insertIdx = constructor.Body.Instructions.Count - 1;
constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Nop));
constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Ldarg_0));
constructor.Body.Instructions.Insert(
insertIdx++,
Instruction.Create(
OpCodes.Newobj,
MakeGeneric(
typeDef.Module.Import(
typeDef.Module.Import(typeof(List<>))
.MakeGenericInstanceType(propDef.PropertyType)
.Resolve()
.GetConstructors()
.First(c => !c.HasParameters)),
((GenericInstanceType)propDef.PropertyType).GenericArguments.First())));
constructor.Body.Instructions.Insert(insertIdx, Instruction.Create(OpCodes.Call, propDef.SetMethod));
}