Is it possible to pass interpolated strings as parameter to a method?

青春壹個敷衍的年華 提交于 2019-12-01 01:45:30

问题


I have started to use Interpolated Strings (new feature of C# 6) and it is really useful and gracefully. But according to my needs I have to pass format of string to a method as a parameter. Something like next:

MyMethod(string format)

In the past, I used it in the next way:

MyMethod("AAA{0:00}")

Now I tried this code:

MyMethod($"AAA{i:00}")

But this doesn't work, because i is created inside of the method and is out of scope in this context.

Is it possible to use any trick for passing interpolated strings as a parameter to a method?


回答1:


You cannot do that, and that would not be a very good idea either - it means you are using local variables from another method.
This would defeat the purpose of this feature - to have compiler verifiable string interpolation and binding to local variables.

C# has several good alternatives. For example, using a Func:

public void MyMethod(Func<int,string> formatNumber)
{
    int i = 3;
    var formatted = formatNumber(i);
}

use as:

MyMethod(number => $"AAA{number:00}");

This is better than what you have today - where you have the format string(s?) and its usage in different places.

If you have more than a single variable this approach can scale, but consider grouping them into a class (or a struct) - the func will look much better, and your code will be more readable. This class can also override .ToString(), which might be useful for you.

Another simple option is to pass just the format: MyMethod("00") and i.ToString(format), but that only works in your presumably simplified example.




回答2:


There are several ways you can do this.

The first one is to add another method overload that takes a FormattableString (a new typed used by one of the variants of string interpollation) and calls the original one:

public void MyMethod(FormattableString fs)
{
    MyMethod(fs.Format);
}

And you also have access to the arguments if you need to.

If you only need the format, just create an extension method to extract it:

public static string AsFormat(FormattableString fs)
{
    return fs.Format;
}



回答3:


Several years since this was asked however I came across this via Google while searching for an answer that I already had in my head, but couldn't get my mind around the implementation/syntax.

My solution is further to what @Kobi supplied...

public class Person
    {
        public int Id {get; set;}
        public DateTime DoB {get;set;}
        public String Name {get;set;}
    }

public class MyResolver
{
    public Func<Person, string> Resolve {get;set;}
    public string ResolveToString<T>(T r) where T : Person
    {
        return this.Resolve(r);
    }
}

// code to use here...
var employee = new Person();
employee.Id = 1234;
employee.Name = "John";
employee.DoB = new DateTime(1977,1,1);

var resolver = new MyResolver();
resolver.Resolve = x => $"This person is called {x.Name}, DoB is {x.DoB.ToString()} and their Id is {x.Id}";

Console.WriteLine(resolver.ResolveToString(employee));

I haven't tested the above for syntax errors but hopefully you get the idea and you can now just define "how" you want the string to look but leave the blanks to be filled in at runtime depending upon the values in your Person object.

Cheers,

Wayne




回答4:


It is not possible. But the alternate way is

public static string MyMethod(string format, params object[] args)
{
  if (format == null || args == null)
    throw new ArgumentNullException(format == null ? "format" : "args");
  return string.Format((IFormatProvider) null, format, args);
}

EDIT:

MyMethod("The number is {0}", 12);

EDIT2:

public static string MyMethod(string format)
{
    var i = 12;
    var result = string.Format(null, format, i);
    return result;
}
MyMethod("{0:000}");


来源:https://stackoverflow.com/questions/31987232/is-it-possible-to-pass-interpolated-strings-as-parameter-to-a-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!