get { return _encoding; }
set
{
- ArgumentNullException.ThrowIfNull(value, nameof(value));
+ ArgumentNullException.ThrowIfNull(value);
_encoding = value;
}
}
public byte[] DownloadData(Uri address)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
StartOperation();
try
public void DownloadFile(Uri address, string fileName)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(fileName);
WebRequest? request = null;
FileStream? fs = null;
public Stream OpenRead(Uri address)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
WebRequest? request = null;
StartOperation();
public Stream OpenWrite(Uri address, string? method)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
if (method == null)
{
method = MapToDefaultMethod(address);
public byte[] UploadData(Uri address, string? method, byte[] data)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
public byte[] UploadFile(Uri address, string? method, string fileName)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(fileName);
if (method == null)
{
method = MapToDefaultMethod(address);
public byte[] UploadValues(Uri address, string? method, NameValueCollection data)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
public string UploadString(Uri address, string? method, string data)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
public string DownloadString(Uri address)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
StartOperation();
try
private Uri GetUri(string address)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
Uri? uri;
if (_baseAddress != null)
private Uri GetUri(Uri address)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
Uri? uri = address;
public void OpenReadAsync(Uri address, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
public void OpenWriteAsync(Uri address, string? method, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
if (method == null)
{
method = MapToDefaultMethod(address);
public void DownloadStringAsync(Uri address, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
public void DownloadDataAsync(Uri address, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
+ ArgumentNullException.ThrowIfNull(address);
AsyncOperation asyncOp = StartAsyncOperation(userToken);
try
public void DownloadFileAsync(Uri address, string fileName, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(fileName);
FileStream? fs = null;
AsyncOperation asyncOp = StartAsyncOperation(userToken);
public void UploadStringAsync(Uri address, string? method, string data, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
public void UploadDataAsync(Uri address, string? method, byte[] data, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
public void UploadFileAsync(Uri address, string? method, string fileName, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(fileName, nameof(fileName));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(fileName);
if (method == null)
{
method = MapToDefaultMethod(address);
public void UploadValuesAsync(Uri address, string? method, NameValueCollection data, object? userToken)
{
- ArgumentNullException.ThrowIfNull(address, nameof(address));
- ArgumentNullException.ThrowIfNull(data, nameof(data));
+ ArgumentNullException.ThrowIfNull(address);
+ ArgumentNullException.ThrowIfNull(data);
if (method == null)
{
method = MapToDefaultMethod(address);
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type,
Type[] types)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetConstructor(types);
}
public static ConstructorInfo[] GetConstructors(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetConstructors();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetConstructors(bindingAttr);
}
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetDefaultMembers();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] this Type type,
string name)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetEvent(name);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetEvent(name, bindingAttr);
}
public static EventInfo[] GetEvents(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetEvents();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetEvents(bindingAttr);
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] this Type type,
string name)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetField(name);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetField(name, bindingAttr);
}
public static FieldInfo[] GetFields(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetFields();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetFields(bindingAttr);
}
public static Type[] GetGenericArguments(this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetGenericArguments();
}
public static Type[] GetInterfaces(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetInterfaces();
}
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type,
string name)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMember(name);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMember(name, bindingAttr);
}
| DynamicallyAccessedMemberTypes.PublicConstructors
| DynamicallyAccessedMemberTypes.PublicNestedTypes)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMembers();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMembers(bindingAttr);
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] this Type type,
string name)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMethod(name);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMethod(name, bindingAttr);
}
string name,
Type[] types)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMethod(name, types);
}
public static MethodInfo[] GetMethods(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMethods();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetMethods(bindingAttr);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetNestedType(name, bindingAttr);
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetNestedTypes(bindingAttr);
}
public static PropertyInfo[] GetProperties(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] this Type type)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperties();
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] this Type type,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperties(bindingAttr);
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] this Type type,
string name)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperty(name);
}
string name,
BindingFlags bindingAttr)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperty(name, bindingAttr);
}
string name,
Type? returnType)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperty(name, returnType);
}
Type? returnType,
Type[] types)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.GetProperty(name, returnType, types);
}
public static bool IsAssignableFrom(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] Type? c)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.IsAssignableFrom(c);
}
public static bool IsInstanceOfType(this Type type, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] object? o)
{
- ArgumentNullException.ThrowIfNull(type, nameof(type));
+ ArgumentNullException.ThrowIfNull(type);
return type.IsInstanceOfType(o);
}
}
[RequiresUnreferencedCode("Types might be removed")]
public static Type[] GetExportedTypes(this Assembly assembly)
{
- ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly);
return assembly.GetExportedTypes();
}
public static Module[] GetModules(this Assembly assembly)
{
- ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly);
return assembly.GetModules();
}
[RequiresUnreferencedCode("Types might be removed")]
public static Type[] GetTypes(this Assembly assembly)
{
- ArgumentNullException.ThrowIfNull(assembly, nameof(assembly));
+ ArgumentNullException.ThrowIfNull(assembly);
return assembly.GetTypes();
}
}
{
public static MethodInfo? GetAddMethod(this EventInfo eventInfo)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetAddMethod();
}
public static MethodInfo? GetAddMethod(this EventInfo eventInfo, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetAddMethod(nonPublic);
}
public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetRaiseMethod();
}
public static MethodInfo? GetRaiseMethod(this EventInfo eventInfo, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetRaiseMethod(nonPublic);
}
public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetRemoveMethod();
}
public static MethodInfo? GetRemoveMethod(this EventInfo eventInfo, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(eventInfo, nameof(eventInfo));
+ ArgumentNullException.ThrowIfNull(eventInfo);
return eventInfo.GetRemoveMethod(nonPublic);
}
}
/// <remarks>This maybe</remarks>
public static bool HasMetadataToken(this MemberInfo member)
{
- ArgumentNullException.ThrowIfNull(member, nameof(member));
+ ArgumentNullException.ThrowIfNull(member);
try
{
/// </exception>
public static int GetMetadataToken(this MemberInfo member)
{
- ArgumentNullException.ThrowIfNull(member, nameof(member));
+ ArgumentNullException.ThrowIfNull(member);
int token = GetMetadataTokenOrZeroOrThrow(member);
{
public static MethodInfo GetBaseDefinition(this MethodInfo method)
{
- ArgumentNullException.ThrowIfNull(method, nameof(method));
+ ArgumentNullException.ThrowIfNull(method);
return method.GetBaseDefinition();
}
}
{
public static bool HasModuleVersionId(this Module module)
{
- ArgumentNullException.ThrowIfNull(module, nameof(module));
+ ArgumentNullException.ThrowIfNull(module);
return true; // not expected to fail on platforms with Module.ModuleVersionId built-in.
}
public static Guid GetModuleVersionId(this Module module)
{
- ArgumentNullException.ThrowIfNull(module, nameof(module));
+ ArgumentNullException.ThrowIfNull(module);
return module.ModuleVersionId;
}
}
{
public static MethodInfo[] GetAccessors(this PropertyInfo property)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetAccessors();
}
public static MethodInfo[] GetAccessors(this PropertyInfo property, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetAccessors(nonPublic);
}
public static MethodInfo? GetGetMethod(this PropertyInfo property)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetGetMethod();
}
public static MethodInfo? GetGetMethod(this PropertyInfo property, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetGetMethod(nonPublic);
}
public static MethodInfo? GetSetMethod(this PropertyInfo property)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetSetMethod();
}
public static MethodInfo? GetSetMethod(this PropertyInfo property, bool nonPublic)
{
- ArgumentNullException.ThrowIfNull(property, nameof(property));
+ ArgumentNullException.ThrowIfNull(property);
return property.GetSetMethod(nonPublic);
}
}
{
public static class CallerArgumentExpressionAttributeTests
{
- public static string IntParamMethod(int val, [CallerArgumentExpression("val")] string expr = null)
+ [Theory]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData("paramName")]
+ public static void Ctor_ParameterName_Roundtrip(string value)
{
- return expr;
- }
-
- [Theory, InlineData("testParamName"), InlineData(""), InlineData(null)]
- public static void ArgumentToCallerArgumentExpressionSetsParameterNameProperty(string paramName)
- {
- var attr = new CallerArgumentExpressionAttribute(paramName);
-
- Assert.Equal(paramName, attr.ParameterName);
- }
-
- [Fact]
- public static void SuppliedArgumentOverridesExpression()
- {
- int notVal = 0;
-
- string suppliedVal = "supplied value";
- Assert.Equal(suppliedVal, IntParamMethod(notVal, suppliedVal));
-
- Assert.Equal(IntParamMethod(notVal), IntParamMethodPassthrough(notVal));
- }
-
- private static string IntParamMethodPassthrough(int val, [CallerArgumentExpression("val")] string expr = null)
- {
- return IntParamMethod(val, expr);
- }
-
- [Fact]
- public static void InvalidParameterName()
- {
- int notVal = 0;
-
- Assert.Null(InvalidParameterNameMethod(notVal));
- }
-
- private static string InvalidParameterNameMethod(int val, [CallerArgumentExpression("notVal")] string expr = null)
- {
- return expr;
- }
-
- [Fact]
- public static void NullParameterName()
- {
- int notVal = 0;
-
- Assert.Null(NullParameterNameMethod(notVal));
- }
-
- private static string NullParameterNameMethod(int val, [CallerArgumentExpression(null)] string expr = null)
- {
- return expr;
- }
-
- [Fact]
- public static void OverloadedMethodPrecedence()
- {
- int notVal = 0;
-
- Assert.Equal(OverloadedMethodReturn, OverloadedMethod(notVal));
- }
-
- private const string OverloadedMethodReturn = "not CallerArgumentExpression";
-
- private static string OverloadedMethod(int val)
- {
- return OverloadedMethodReturn;
- }
-
- private static string OverloadedMethod(int val, [CallerArgumentExpression(null)] string expr = null)
- {
- return expr;
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void SimpleExpression()
- {
- int notVal = 0;
-
- Assert.Equal("notVal", IntParamMethod(notVal));
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void ComplexExpression()
- {
- int x = 5;
-
- Assert.Equal("Math.Min(x + 20, x * x)",
- IntParamMethod(Math.Min(x + 20, x * x)));
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void SurroundingWhitespaceHandling()
- {
- int notVal = 0;
-
- Assert.Equal("notVal", IntParamMethod(notVal));
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void InternalWhitespaceHandling()
- {
- int notVal = 0;
-
- Assert.Equal("notVal + 20", IntParamMethod(notVal + 20));
-
- Assert.Equal(@"Math.Min(notVal * 2,
- notVal + 20)",
- IntParamMethod(Math.Min(notVal * 2,
- notVal + 20)));
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void InternalCommentHandling()
- {
- int notVal = 0;
-
- Assert.Equal("notVal + /*comment*/20", IntParamMethod(notVal + /*comment*/20));
- Assert.Equal("notVal + 20 //comment",
- IntParamMethod(notVal + 20 //comment
- ));
+ var caea = new CallerArgumentExpressionAttribute(value);
+ Assert.Equal(value, caea.ParameterName);
}
[Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void OptionalParameterHandling()
+ public static void BasicTest()
{
- string suppliedVal = "supplied value";
- Assert.Equal("suppliedVal", OptionalParamMethod(suppliedVal));
- Assert.Equal("suppliedVal", OptionalParamMethod(val: suppliedVal));
-
- Assert.Equal("StringConst + \" string literal\"", OptionalParamMethod());
-
- Assert.Equal("\"no file\"", CompilerSuppliedParamMethod());
+ // Just a quick test to validate basic behavior. Compiler tests validate it fully.
+ Assert.Equal("\"hello\"", GetValue("hello"));
+ Assert.Equal("3 + 2", GetValue(3 + 2));
+ Assert.Equal("new object()", GetValue(new object()));
}
- private const string StringConst = "hello";
-
- private static string OptionalParamMethod(string val = StringConst + " string literal", [CallerArgumentExpression("val")] string expr = null)
- {
- return expr;
- }
-
- private static string CompilerSuppliedParamMethod([CallerFilePath] string val = "no file", [CallerArgumentExpression("val")] string expr = null)
- {
- return expr;
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void ExtensionMethodThisParameterHandling()
- {
- int notVal = 0;
-
- Assert.Equal("notVal", notVal.ExtensionMethod());
- }
-
- private static string ExtensionMethod(this int val, [CallerArgumentExpression("val")] string expr = null)
- {
- return expr;
- }
-
- [Fact]
- [ActiveIssue("https://github.com/dotnet/roslyn/issues/19605")]
- public static void InstanceMethodThisHandling()
- {
- var instance = new InstanceTest();
-
- Assert.Equal("instance", instance.Method());
- Assert.Equal("new InstanceTest()", new InstanceTest().Method());
- Assert.Equal("(instance ?? new InstanceTest())", (instance ?? new InstanceTest()).Method());
-
- Assert.Equal("", instance.NoThisMethodCaller());
- Assert.Equal("this", instance.ThisMethodCaller());
- }
-
- private class InstanceTest
- {
- public string NoThisMethodCaller()
- {
- return Method();
- }
-
- public string ThisMethodCaller()
- {
- return this.Method();
- }
-
- public string Method([CallerArgumentExpression("this")] string expr = null)
- {
- return expr;
- }
- }
+ private static string GetValue(object argument, [CallerArgumentExpression("argument")] string expr = null) => expr;
}
}