possible null assignment in Dictionary Contains Key - c#

Getting a warning on "!redirectsDictionary.ContainsKey(autoRedirect.Key)"
asp.net possible null assignment to entity marked with "notnull" attribute.
Just wondering what that's about ?
private static readonly Dictionary<string, Redirect> AutoRedirectsDictionary = new Dictionary<string, Redirect>();
foreach (var r in db.SelectItems("fast:/sitecore/content/Redirects Root//*[##templatename='Auto Redirect']"))
{
GenerateRedirects(Context.Database.GetItem(r.Fields["Root Node"].Value), r["URL Prefix"]);
foreach (var autoRedirect in AutoRedirectsDictionary)
{
if (!string.IsNullOrEmpty(autoRedirect.Key) & !redirectsDictionary.ContainsKey(autoRedirect.Key))
{
//Add to dictionary
redirectsDictionary.Add(autoRedirect.Key, autoRedirect.Value);
}
}
}
public static void GenerateRedirects(Item redirectFolder, string urlPrefix)
{
if (redirectFolder == null)
return;
var childList = redirectFolder.GetChildren();
foreach (Item child in childList)
{
if (Utilities.HasFieldValue(child, FieldToFind))
{
var shortcutUrl = urlPrefix + child.Fields[FieldToFind].Value.ToLower();
if (!string.IsNullOrEmpty(shortcutUrl) && !AutoRedirectsDictionary.ContainsKey(shortcutUrl))
{
AutoRedirectsDictionary.Add(shortcutUrl,
new Redirect(String.Empty, child, true));
}
}
else
{
GenerateRedirects(child, urlPrefix);
}
}
}

It might have to do with your use of the single & operator. A single & will not use short-circuiting to bypass a statement but will instead choose the path to execute after all expressions have been evaluated. So even though you're checking !string.IsNullOrEmpty(autoRedirect.Key) prior to the ContainsKey call, both expressions will be evaluated first and then the path of execution will be decided.
Edited as I realized I didn't truly answer your specific question (and you may already know this) but !redirectsDictionary.ContainsKey(autoRedirect.Key) will throw an exception if the key is null. Since the datatype for the key is a string there is a possibility it will throw an exception if it is null, hence the warning.

Related

Check whether property is readonly

I have this code using reflection and I decided to replace it by FastMember.
This is my code:
var VehicleType = TypeAccessor.Create(res.GetType());
var vehicleFastMember = ObjectAccessor.Create(res);
foreach (var kvp in dictionary)
{
if (kvp.Key == "Identifier") continue;
object value = kvp.Value;
if (VehicleType.GetMembers().FirstOrDefault(prop => prop.Name == kvp.Key) != null)
{
// here inside if i want to check whether property is not readonly,
// I am afraid of runtime exception that readonly cannot be overwritten.
**if (vehicleFastMember[kvp.Key].)**
{
vehicleFastMember[kvp.Key] = kvp.Value;
}
}
}
By reflection:
That row with stars would be solved by this line:
if (property?.CanWrite ?? false)
Does FastMember offer some elegant solution too?
According to the source code of fast-member's MemberSet.Member, CanRead/CanWrite are calling the same methods you are trying to call (PropertyInfo.Can[Read/Write]).
It's also worth noting that instead of determining if the property is writable, you could simply wrap the assignment into a try/catch, and catch the ReadOnlyException that would emerge and continue with the loop.

Roslyn: Check if method parameter can not be null

Using Roslyn, my aim is to check if a method parameter is checked for not being null before the parameter is dereferenced. This check can be in a submethod of course.
My approach is to get the first dereferencing of the parameter and search the syntax tree between that and the method start for null checks. How can I do some kind of control flow analysis to determine if the first dereferencing of the parameter can be reached with the parameter being null?
This is way too broad question, with a little explanation what is your final goal. Are you trying to detect null-pointer exceptions before they even happen, 100%? (Pretty much impossible)
I have written static analysis myself few months ago, I didn't use roslyn, but this doesn't matter.
Check this out to get you possibly started - it's reporting warnings when there are unused variables:
internal class UnUsedVariableWarningDefinition : ICodeIssue
{
public IEnumerable<IssueReport> Analyze()
{
var usageMap = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
var variableMap = new Dictionary<string, IdentifierNode>(StringComparer.InvariantCultureIgnoreCase);
foreach (var node in NodeAnalyzerHelper.FindNodesDfs(Root))
{
var assignmentNode = node as AssignmentNode;
if (assignmentNode != null)
{
var variableNode = assignmentNode.Identifier;
int usages;
if (!usageMap.TryGetValue(variableNode.Identifier, out usages))
{
usageMap[variableNode.Identifier] = 0;
variableMap[variableNode.Identifier] = variableNode;
}
}
else
{
// not really an assignmentNode,
// let's see if we have detected the usage of IdentifierNode somewhere.
var variableNode = node as IdentifierNode;
if (variableNode != null)
{
if (usageMap.ContainsKey(variableNode.Identifier))
usageMap[variableNode.Identifier]++;
}
}
}
foreach (var node in usageMap.Where(x => x.Value == 0).Select(x => variableMap[x.Key]))
{
yield return node.ConstructWarning("No usages of this variable found. Are you sure this is needed?");
}
}
}
Notice that FindNodesDfs() is basically a syntax tree walker, which walks syntax nodes depth-first style. What it does is just scans AssigfnmentNodes and puts them to Dictionary, as soon as it identifies IdentifierNode, it checks the dictionary if it has previously encountered assignment, or not. It's a bit similiar what you're trying to do, I guess.

How to safely check if a dynamic object has a field or not

I'm looping through a property on a dynamic object looking for a field, except I can't figure out how to safely evaluate if it exists or not without throwing an exception.
foreach (dynamic item in routes_list["mychoices"])
{
// these fields may or may not exist
int strProductId = item["selectedProductId"];
string strProductId = item["selectedProductCode"];
}
using reflection is better than try-catch, so this is the function i use :
public static bool doesPropertyExist(dynamic obj, string property)
{
return ((Type)obj.GetType()).GetProperties().Where(p => p.Name.Equals(property)).Any();
}
then..
if (doesPropertyExist(myDynamicObject, "myProperty")){
// ...
}
This is gonna be simple. Set a condition which checks the value is null or empty. If the value is present, then assign the value to the respective datatype.
foreach (dynamic item in routes_list["mychoices"])
{
// these fields may or may not exist
if (item["selectedProductId"] != "")
{
int strProductId = item["selectedProductId"];
}
if (item["selectedProductCode"] != null && item["selectedProductCode"] != "")
{
string strProductId = item["selectedProductCode"];
}
}
You need to surround your dynamic variable with a try catch, nothing else is the better way in makking it safe.
try
{
dynamic testData = ReturnDynamic();
var name = testData.Name;
// do more stuff
}
catch (RuntimeBinderException)
{
// MyProperty doesn't exist
}

Not all code paths return a value

I am having this Linq To SQL query which is taking Customer Category from database.The CustCategory will be defined already.Here is the query.
public IList<string> GetAccountType()
{
using (var db = new DataClasses1DataContext())
{
var acctype = db.mem_types.Select(account=>account.CustCategory).Distinct().ToList();
if (acctype != null)
{
return acctype;
}
}
}
Currently I am getting an error that Not all code paths return a value.If I am always certain that the value is there in the database then do I need to check for null,If I need to check for null then how do I handle this.
Can anyone help me with this.
Any suggestions are welcome.
Since Enumerable.ToList never returns null (see the Return Value section of the documentation), you can safely remove the if.
EDIT: Note that, no matter what your database contains, acctype will never be null:
If no value is found in the database, the return value will be an empty list (which is different than null).
If one record is found and its value is null, the return value will be a valid list with one entry, whose value is null. Still, the list itself is not null.
What happens if:
if (acctype != null)
Is null? What is your method supposed to return?
You need to return something
This is not about LINQ to SQL, the method GetAccountType() must return IList<string>. You should return return acctype; and then check this returned list later using Any(), something like:
if(GetAccountType.Any()){
//not empty
}
How about something like this for a fairly clean and readable solution?:
(Note, updated: removed the check for null, since it would clearly not have any effect).
public IList<string> GetAccountType()
{
var acctype = new List<string>();
using (var db = new DataClasses1DataContext())
{
acctype = db.mem_types.Select(
account=>account.CustCategory).Distinct().ToList();
}
return acctype;
}
You need to return a value from your function:
public IList<string> GetAccountType()
{
using (var db = new DataClasses1DataContext())
{
var acctype = db.mem_types.Select(account=>account.CustCategory).Distinct().ToList();
if (acctype != null)
{
return acctype;
}
}
return acctype;
}

Using linq for validation/ throwing exception

This may be the wrong way to do things(!) but i am just wondering ....
I am using
//check that all transform fields have corresponding database columns
foreach (string sDatabaseFieldName in l_sDatabaseFieldNames)
{
bool bFound = false;
foreach (SqlParameter sqlp in sqlcmdAll.Parameters)
{
if (sqlp.SourceColumn == sDatabaseFieldName) bFound = true;
}
if (!bFound)
{
throw new Exception(string.Format("Transform field {0} does not have a corresponding column in the target table.", sDatabaseFieldName));
}
}
where l_sDatabaseFieldNames is a List< string> and sqlcmdAll is an insert SqlCommand with validated parameter names.
I want to throw an exception if an item in l_sDatabaseFieldNames is not in any sqlcmdAll.Parameters.SourceColumn. In other words all column names contained in l_sDatabaseFieldNames should also have a matching parameter (compared using SourceColumn property).
I can also use
bool bFound = l_sDatabaseFieldNames.All(sDBFN => sqlcmdAll.Parameters.Cast<SqlParameter>().Any(param => sDBFN == param.SourceColumn));
but i only get a true/false result.
Can i use a combination of the two techniques and throw an exception from within the linq query if an item is in l_sDatabaseFieldNames, but not in any sqlcmdAll.Parameters.SourceColumn?
Thank you in advance,
James.
What exception would you expect it to throw? I feel this is better for you to take the result and throw the exception you want to throw:
if (!l_sDatabaseFieldNames.All(sDBFN => sqlcmdAll.Parameters.Cast<SqlParameter>().Any(param => sDBFN == param.SourceColumn)))
{
throw new YourCustomException("Your custom message");
}
Of course, if this is for debugging purposes only to verify a condition in testing and you don't need it to go into the actual release code you can use an assertion:
Debug.Assert(l_sDatabaseFieldNames.All(sDBFN => sqlcmdAll.Parameters.Cast<SqlParameter>().Any(param => sDBFN == param.SourceColumn)));
* UPDATE *
Based on your comment, you have a few choices, basically. We could bastardize a select clause but I don't like this as it feels kinda weird throwing in a projection. Sadly Linq doesn't already have a ForEach() that lets you perform an Action<T>, so you can either write your own ForEach() for IEnumerable<T>, or you can use ToList() to convert the sequence to a List<T> which does have a ForEach():
sqlcmdAll.Parameters.Cast<SqlParameter>().ToList().ForEach(p =>
{
if (!l_sDatabaseFieldNames.Contains(p.SourceColumn))
{
throw new Exception("Could not find " + p.SourceColumn);
}
});
If you don't mind writing your own extension method, you can add this to your library (comes in very handy) to give ForEach() to IEnumerable:
public static class EnumerableExtensions
{
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source,
Action<T> action)
{
if (source == null) throw new ArgumentNullException("source");
foreach (T element in source)
{
action(element);
}
return source;
}
}
Which would let you do this instead of using ToList():
sqlcmdAll.Parameters.Cast<SqlParameter>().ForEach(p =>
{
if (!l_sDatabaseFieldNames.Contains(p.SourceColumn))
{
throw new Exception("Could not find " + p.SourceColumn);
}
});
You could select the first non matching parameter
SqlParameter sqlp = sqlcmdAll.Parameters.Cast<SqlParameter>().Where(param => !l_sDatabaseFieldNames.Contains(param.SourceColumn)).FirstOrDefault();
if (sqlp != null)
throw new Exception(string.Format("Transform field {0} does not have a corresponding column in the target table.", sqlp.SourceColumn));

Categories

Resources