本文整理汇总了C#中IType.GetFields方法的典型用法代码示例。如果您正苦于以下问题:C# IType.GetFields方法的具体用法?C# IType.GetFields怎么用?C# IType.GetFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IType
的用法示例。
在下文中一共展示了IType.GetFields方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FindFields
static IEnumerable<FieldWrapper> FindFields(IType sourceClass)
{
int i = 0;
foreach (var f in sourceClass.GetFields().Where(field => !field.IsConst
&& field.ReturnType != null)) {
yield return new FieldWrapper(f) { Index = i };
i++;
}
}
示例2: ParseMinimalFields
internal static void ParseMinimalFields(SDType sdType, IType type)
{
var fields = type.GetFields(null, GetMemberOptions.IgnoreInheritedMembers);
foreach (var field in fields)
{
var parsedField = GetMinimalParsedField(field);
if (sdType.Fields.SingleOrDefault(f => f.Name == parsedField.Name) == null)
{
sdType.Fields.Add(parsedField);
}
}
}
示例3: ParseFields
internal void ParseFields(SDType sdType, IType type)
{
var fields = type.GetFields(null, GetMemberOptions.IgnoreInheritedMembers);
foreach (var field in fields)
{
if (!_excludedIdentifiers.Contains(field.GetIdentifier()))
{
var parsedField = GetParsedField(field);
if (sdType.Fields.SingleOrDefault(f => f.Name == parsedField.Name) == null)
{
sdType.Fields.Add(parsedField);
}
}
}
}
示例4: CreateCtorParams
IEnumerable<PropertyOrFieldWrapper> CreateCtorParams(IType sourceType)
{
int i = 0;
foreach (var f in sourceType.GetFields().Where(field => !field.IsConst
&& field.IsStatic == sourceType.GetDefinition().IsStatic
&& field.DeclaringType.FullName == sourceType.FullName
&& field.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(f) { Index = i };
i++;
}
foreach (var p in sourceType.GetProperties().Where(prop => prop.CanSet && !prop.IsIndexer
&& prop.IsAutoImplemented()
&& prop.IsStatic == sourceType.GetDefinition().IsStatic
&& prop.DeclaringType.FullName == sourceType.FullName
&& prop.ReturnType != null)) {
yield return new PropertyOrFieldWrapper(p) { Index = i };
i++;
}
}
示例5: FindField
IField FindField (IType cls, string name)
{
foreach (IField field in cls.GetFields ())
if (field.Name == name)
return field;
return null;
}
示例6: GetEnumLiterals
public IEnumerable<IField> GetEnumLiterals(IType type)
{
if (type.Kind != TypeKind.Enum)
throw new ArgumentException ("Type is no enum.");
foreach (var field in type.GetFields (f => f.IsConst && f.IsPublic))
yield return field;
}
示例7: AppendConstant
void AppendConstant (StringBuilder sb, IType constantType, object constantValue)
{
if (constantValue is string) {
sb.Append (Highlight ("\"" + constantValue + "\"", colorStyle.String));
return;
}
if (constantValue is char) {
sb.Append (Highlight ("'" + constantValue + "'", colorStyle.String));
return;
}
if (constantValue is bool) {
sb.Append (Highlight ((bool)constantValue ? "true" : "false", colorStyle.KeywordConstants));
return;
}
if (constantValue == null) {
if (constantType.Kind == TypeKind.Struct) {
// structs can never be == null, therefore it's the default value.
sb.Append (Highlight ("default", colorStyle.KeywordSelection) + "(" + GetTypeReferenceString (constantType) + ")");
} else {
sb.Append (Highlight ("null", colorStyle.KeywordConstants));
}
return;
}
while (NullableType.IsNullable (constantType))
constantType = NullableType.GetUnderlyingType (constantType);
if (constantType.Kind == TypeKind.Enum) {
foreach (var field in constantType.GetFields ()) {
if (field.ConstantValue == constantValue){
sb.Append (GetTypeReferenceString (constantType) + "." + CSharpAmbience.FilterName (field.Name));
return;
}
}
sb.Append ("(" + GetTypeReferenceString (constantType) + ")" + Highlight (constantValue.ToString (), colorStyle.Number));
return;
}
sb.Append (Highlight (constantValue.ToString (), colorStyle.Number));
}
示例8: ConvertConstantValue
public Expression ConvertConstantValue(IType type, object constantValue)
{
if (type == null)
throw new ArgumentNullException("type");
if (constantValue == null) {
if (type.IsReferenceType == true)
return new NullReferenceExpression();
else
return new DefaultValueExpression(ConvertType(type));
} else if (type.Kind == TypeKind.Enum) {
// TODO: Improve flags parameter Foo.A | Foo.B etc.
foreach (var member in type.GetFields(m => m.IsPublic && m.IsConst)) {
if (Equals (member.ConstantValue, constantValue)) {
return new MemberReferenceExpression(ConvertType(type), member.Name);
}
}
return new CastExpression(ConvertType(type), ConvertConstantValue(type.GetDefinition().EnumUnderlyingType, constantValue));
} else {
return new PrimitiveExpression(constantValue);
}
}
示例9: AddEnumMembers
void AddEnumMembers(CompletionDataWrapper completionList, IType resolvedType, CSharpResolver state)
{
if (resolvedType.Kind != TypeKind.Enum) {
return;
}
string typeString = GetShortType(resolvedType, state);
if (typeString.Contains(".")) {
completionList.AddType(resolvedType, typeString);
}
foreach (var field in resolvedType.GetFields ()) {
if (field.IsConst || field.IsStatic) {
completionList.Result.Add(factory.CreateEntityCompletionData(
field,
typeString + "." + field.Name
)
);
}
}
DefaultCompletionString = typeString;
}
示例10: Lookup
/// <summary>
/// Performs a member lookup.
/// </summary>
public ResolveResult Lookup(IType type, string name, IList<IType> typeArguments, bool isInvocation)
{
int typeArgumentCount = typeArguments.Count;
List<IType> types = new List<IType>();
List<IMember> members = new List<IMember>();
if (!isInvocation) {
// Consider nested types only if it's not an invocation. The type parameter count must match in this case.
Predicate<ITypeDefinition> typeFilter = delegate (ITypeDefinition d) {
return d.TypeParameterCount == typeArgumentCount && d.Name == name && IsAccessible(d, true);
};
types.AddRange(type.GetNestedTypes(context, typeFilter));
}
bool allowProtectedAccess = IsProtectedAccessAllowed(type);
if (typeArgumentCount == 0) {
Predicate<IMember> memberFilter = delegate(IMember member) {
return !member.IsOverride && member.Name == name && IsAccessible(member, allowProtectedAccess);
};
members.AddRange(type.GetMethods(context, memberFilter.SafeCast<IMember, IMethod>()).SafeCast<IMethod, IMember>());
members.AddRange(type.GetProperties(context, memberFilter.SafeCast<IMember, IProperty>()).SafeCast<IProperty, IMember>());
members.AddRange(type.GetFields(context, memberFilter.SafeCast<IMember, IField>()).SafeCast<IField, IMember>());
members.AddRange(type.GetEvents(context, memberFilter.SafeCast<IMember, IEvent>()).SafeCast<IEvent, IMember>());
if (isInvocation)
members.RemoveAll(m => !IsInvocable(m, context));
} else {
// No need to check for isInvocation/isInvocable here:
// we filter out all non-methods
Predicate<IMethod> memberFilter = delegate(IMethod method) {
return method.TypeParameters.Count == typeArgumentCount
&& !method.IsOverride && method.Name == name && IsAccessible(method, allowProtectedAccess);
};
members.AddRange(type.GetMethods(context, memberFilter).SafeCast<IMethod, IMember>());
}
// TODO: can't members also hide types?
RemoveTypesHiddenByOtherTypes(types);
// remove members hidden by types
for (int i = 0; i < types.Count; i++) {
ITypeDefinition d = GetDeclaringTypeDef(types[i]);
if (d != null)
members.RemoveAll(m => d.IsDerivedFrom(m.DeclaringTypeDefinition, context));
}
// remove members hidden by other members
for (int i = members.Count - 1; i >= 0; i--) {
ITypeDefinition d = members[i].DeclaringTypeDefinition;
IMethod mi = members[i] as IMethod;
// nested loop depends on the fact that the members of more derived classes appear later in the list
for (int j = i + 1; j < members.Count; j++) {
if (mi != null) {
IMethod mj = members[j] as IMethod;
if (mj != null && !ParameterListComparer.Instance.Equals(mi, mj))
continue;
}
ITypeDefinition s = members[j].DeclaringTypeDefinition;
if (s != null && s != d && s.IsDerivedFrom(d, context)) {
// members[j] hides members[i]
members.RemoveAt(i);
break;
}
}
}
// remove interface members hidden by class members
if (type is ITypeParameter) {
// this can happen only with type parameters
for (int i = members.Count - 1; i >= 0; i--) {
ITypeDefinition d = members[i].DeclaringTypeDefinition;
if (d.ClassType != ClassType.Interface)
continue;
IMethod mi = members[i] as IMethod;
for (int j = 0; j < members.Count; j++) {
if (mi != null) {
IMethod mj = members[j] as IMethod;
if (mj != null && !ParameterListComparer.Instance.Equals(mi, mj))
continue;
}
ITypeDefinition s = members[j].DeclaringTypeDefinition;
if (s != null && IsNonInterfaceType(s)) {
// members[j] hides members[i]
members.RemoveAt(i);
break;
}
}
}
}
if (types.Count > 0) {
bool isAmbiguous = !(types.Count == 1 && members.Count == 0);
return CreateTypeResolveResult(types[0], isAmbiguous, typeArguments);
}
if (members.Count == 0)
return new UnknownMemberResolveResult(type, name, typeArguments);
IMember firstNonMethod = members.FirstOrDefault(m => !(m is IMethod));
if (members.Count == 1 && firstNonMethod != null)
return new MemberResolveResult(firstNonMethod, context);
//.........这里部分代码省略.........
示例11: AddEnumMembers
public void AddEnumMembers (IType resolvedType, CSharpResolver state, string typeString)
{
if (addedEnums.Contains (resolvedType))
return;
addedEnums.Add (resolvedType);
if (typeString.Contains(".")) {
AddType(resolvedType, typeString);
}
foreach (var field in resolvedType.GetFields ()) {
if (field.IsPublic && (field.IsConst || field.IsStatic)) {
Result.Add(Factory.CreateEntityCompletionData(
field,
typeString + "." + field.Name
)
);
}
}
}
示例12: GetValueTypeInitializer
public static ResolveResult GetValueTypeInitializer(IType ce, NProject Project)
{
var fullName = SkJs.GetEntityJsName(ce);
if (ce.FullName == "System.Nullable")
return Cs.Null();
if (ce is ITypeDefinition)
{
var def = (ITypeDefinition)ce;
if (def.KnownTypeCode != KnownTypeCode.None)
{
if (def.KnownTypeCode == KnownTypeCode.Boolean)
{
return Cs.Value(false, Project);
}
else if (def.KnownTypeCode == KnownTypeCode.Char)
{
return Cs.Value('\0', Project);
}
else if (def.KnownTypeCode == KnownTypeCode.SByte ||
def.KnownTypeCode == KnownTypeCode.Int16 ||
def.KnownTypeCode == KnownTypeCode.Int32 ||
def.KnownTypeCode == KnownTypeCode.Int64 ||
def.KnownTypeCode == KnownTypeCode.UInt16 ||
def.KnownTypeCode == KnownTypeCode.UInt32 ||
def.KnownTypeCode == KnownTypeCode.UInt64 ||
def.KnownTypeCode == KnownTypeCode.Byte ||
def.KnownTypeCode == KnownTypeCode.Decimal ||
def.KnownTypeCode == KnownTypeCode.Double ||
def.KnownTypeCode == KnownTypeCode.Single
)
{
return Cs.Value(0, Project);
}
}
}
if (ce.Kind == TypeKind.Enum)
{
var en = ce;
var enumMembers = en.GetFields();
var defaultEnumMember = enumMembers.Where(t => (t.ConstantValue is int) && (int)t.ConstantValue == 0).FirstOrDefault() ?? enumMembers.FirstOrDefault();
if (defaultEnumMember != null)
return defaultEnumMember.AccessSelf();//.Access().Member(c.CreateTypeRef(en), defaultEnumMember);
else
return null;
}
else if (ce.GetEntityType().FullName == "System.DateTime")
{
var minDateFe = ce.GetFields(t => t.Name == "MinValue").First();
return minDateFe.AccessSelf();// c.Member(c.Class(c.DateTimeType), minDateFe);
}
else
{
return Cs.New(ce);
}
}
示例13: CanConvertToEnum
static bool CanConvertToEnum(ResolveResult rr, IType type)
{
Debug.Assert(rr.IsCompileTimeConstant);
if(type.Kind == TypeKind.Enum)
return type.GetFields().Any((field) => rr.ConstantValue == field.ConstantValue);
else
return false;
}
示例14: InsertFieldInitializersIntoConstructorBody
private void InsertFieldInitializersIntoConstructorBody(IType type, ConstructorDeclaration declaration)
{
// For constructor forwarding, the base class initializer and all field initializers are in the other constructor
if (declaration.Initializer.ConstructorInitializerType == ConstructorInitializerType.This) {
return;
}
var body = declaration.Body;
var insertBefore = body.FirstChild;
foreach (var field in type.GetFields(null, GetMemberOptions.IgnoreInheritedMembers)) {
VariableInitializer variable;
Expression value;
// Ignore non-instance fields
if (field.IsStatic || !context.fields.TryGetValue(field, out variable)) {
continue;
}
// Use the initializer if present
if (variable.Initializer.IsNull) {
value = CreateDefaultValue(field.Type);
} else {
value = variable.Initializer.Clone();
}
// Add an assignment inside the constructor body
body.InsertChildBefore(insertBefore, new NewLineNode(), Roles.NewLine);
body.InsertChildBefore(insertBefore, new ExpressionStatement(new AssignmentExpression(
new MemberReferenceExpression(new ThisReferenceExpression(),
field.Name), value)), BlockStatement.StatementRole);
}
}
示例15: CreateEqualsOverrides
List<MethodDeclaration> CreateEqualsOverrides(IType currentClass)
{
List<MethodDeclaration> methods = new List<MethodDeclaration>();
AstType boolReference = ConvertType(KnownTypeCode.Boolean);
AstType objectReference = ConvertType(KnownTypeCode.Object);
MethodDeclaration method = new MethodDeclaration {
Name = "Equals",
Modifiers = Modifiers.Public | Modifiers.Override,
ReturnType = boolReference
};
method.Parameters.Add(new ParameterDeclaration(objectReference, "obj"));
method.Body = new BlockStatement();
AstType currentType = ConvertType(currentClass);
Expression expr = null;
if (currentClass.Kind == TypeKind.Struct) {
// return obj is CurrentType && Equals((CurrentType)obj);
expr = new IsExpression() {
Expression = new IdentifierExpression("obj"),
Type = currentType.Clone()
};
expr = new ParenthesizedExpression(expr);
expr = new BinaryOperatorExpression(
expr, BinaryOperatorType.ConditionalAnd,
new InvocationExpression(
new IdentifierExpression("Equals"),
new List<Expression> {
new CastExpression(currentType.Clone(), new IdentifierExpression("obj"))
}));
method.Body.Add(new ReturnStatement(expr));
methods.Add(method);
// IEquatable implementation:
method = new MethodDeclaration {
Name = "Equals",
Modifiers = Modifiers.Public,
ReturnType = boolReference.Clone()
};
method.Parameters.Add(new ParameterDeclaration(currentType, "other"));
method.Body = new BlockStatement();
} else {
method.Body.Add(new VariableDeclarationStatement(
currentType.Clone(),
"other",
new IdentifierExpression("obj").CastAs(currentType.Clone())));
method.Body.Add(new IfElseStatement(
new BinaryOperatorExpression(new IdentifierExpression("other"), BinaryOperatorType.Equality, new PrimitiveExpression(null, "null")),
new ReturnStatement(new PrimitiveExpression(false, "false"))));
}
expr = null;
foreach (IField field in currentClass.GetFields()) {
if (field.IsStatic) continue;
if (expr == null) {
expr = TestEquality("other", field);
} else {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd,
TestEquality("other", field));
}
}
foreach (IProperty property in currentClass.GetProperties()) {
if (property.IsStatic || !property.IsAutoImplemented()) continue;
if (expr == null) {
expr = TestEquality("other", property);
} else {
expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ConditionalAnd,
TestEquality("other", property));
}
}
method.Body.Add(new ReturnStatement(expr ?? new PrimitiveExpression(true, "true")));
methods.Add(method);
return methods;
}