Пример #1
0
        public static void AssignNamesToVariables(DecompilerContext context, IEnumerable <ILVariable> parameters, IEnumerable <ILVariable> variables, ILBlock methodBody)
        {
            NameVariables nv = new NameVariables();

            nv.context = context;
            nv.fieldNamesInCurrentType = context.CurrentType.Fields.Select(f => f.Name).ToList();
            // First mark existing variable names as reserved.
            foreach (string name in context.ReservedVariableNames)
            {
                nv.AddExistingName(name);
            }
            foreach (var p in parameters)
            {
                nv.AddExistingName(p.Name);
            }
            foreach (var v in variables)
            {
                if (v.IsGenerated)
                {
                    // don't introduce names for variables generated by ILSpy - keep "expr"/"arg"
                    nv.AddExistingName(v.Name);
                }
                else if (v.OriginalVariable != null && context.Settings.UseDebugSymbols)
                {
                    string varName = v.OriginalVariable.Name;
                    if (string.IsNullOrEmpty(varName) || varName.StartsWith("V_", StringComparison.Ordinal) || !IsValidName(varName))
                    {
                        // don't use the name from the debug symbols if it looks like a generated name
                        v.Name = null;
                    }
                    else
                    {
                        // use the name from the debug symbols
                        // (but ensure we don't use the same name for two variables)
                        v.Name = nv.GetAlternativeName(varName);
                    }
                }
                else
                {
                    v.Name = null;
                }
            }
            // Now generate names:
            foreach (ILVariable p in parameters)
            {
                if (string.IsNullOrEmpty(p.Name))
                {
                    p.Name = nv.GenerateNameForVariable(p, methodBody);
                }
            }
            foreach (ILVariable varDef in variables)
            {
                if (string.IsNullOrEmpty(varDef.Name))
                {
                    varDef.Name = nv.GenerateNameForVariable(varDef, methodBody);
                }
            }
        }
		public static void AssignNamesToVariables(DecompilerContext context, IEnumerable<ILVariable> parameters, IEnumerable<ILVariable> variables, ILBlock methodBody)
		{
			NameVariables nv = new NameVariables();
			nv.context = context;
			nv.fieldNamesInCurrentType = context.CurrentType.Fields.Select(f => f.Name).ToList();
			// First mark existing variable names as reserved.
			foreach (string name in context.ReservedVariableNames)
				nv.AddExistingName(name);
			foreach (var p in parameters)
				nv.AddExistingName(p.Name);
			foreach (var v in variables) {
				if (v.IsGenerated) {
					// don't introduce names for variables generated by ILSpy - keep "expr"/"arg"
					nv.AddExistingName(v.Name);
				} else if (v.OriginalVariable != null && context.Settings.UseDebugSymbols) {
					string varName = v.OriginalVariable.Name;
					if (string.IsNullOrEmpty(varName) || varName.StartsWith("V_", StringComparison.Ordinal) || !IsValidName(varName))
					{
						// don't use the name from the debug symbols if it looks like a generated name
						v.Name = null;
					} else {
						// use the name from the debug symbols
						// (but ensure we don't use the same name for two variables)
						v.Name = nv.GetAlternativeName(varName);
					}
				} else {
					v.Name = null;
				}
			}
			// Now generate names:
			foreach (ILVariable p in parameters) {
				if (string.IsNullOrEmpty(p.Name))
					p.Name = nv.GenerateNameForVariable(p, methodBody);
			}
			foreach (ILVariable varDef in variables) {
				if (string.IsNullOrEmpty(varDef.Name))
					varDef.Name = nv.GenerateNameForVariable(varDef, methodBody);
			}
		}