can configure custom behaviour of built-in compiler exceptions? | SoloLearn: Learn to code for FREE!

+4

can configure custom behaviour of built-in compiler exceptions?

assuming that if u call a property, field or method of an object, if it hasn't it... instead of throw an exception, there is some way of return 0 or null, or execute some routine or event? (apart of implement that behaviour with a method of that class-object ) scenario: got a set of objects (independently if they got all the same inheritance family and level hierarchical level), and they have bool fields. (the bools respond with true or false to the affirmation of its identifier) to avoid that all the type objects got all the possible bools that exist in the whole group of all the rest of brother objects, every object got its own set of relevant bools, and the rest not. so we use an external call asking for its bools, but if are asked for a missing one, then instead of an exception... (understand it?) but the custom exception behaviour only apply for those objects "case"

9/27/2020 11:48:54 PM

Kiwwi#

12 Answers

New Answer

+11

Kiwwi# I'm with Schindlabua as I'm not following the use case scenario and agree with his feedback. That said, I decided to do a little experimenting and believe I have a solution involving dynamic types. Let me know if this is what you were looking for. https://code.sololearn.com/c0KhE05NCS76/?ref=app Review the class defined on Line 88 to set how to create properties at runtime is they don't exist. More specifically, look at the overridden method TryGetMember(). Everything else is to show different scenarios involving classes that inherit from the base dynamic class.

+7

[Follow Up 1 of 5] Kiwwi# I'm glad I was able to help. Sorry for missing your follow up questions. I'll respond to each bullet separately. NOTE: I've regrouped a few of the related questions together. -------- * @$"" -------- There are a few different things going on here. a) @ is the verbatim prefix for string literals. I'm using this to allow for my string to recognize the line break character without having to use the escaped \n character. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim b) $ is the string interpolation token for injecting expressions into the string without having to explicitly use concatenation or some position based composite formatting method like string.format("{0}", name). https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated c) Both tokens can be used together to enable verbatim string literals with interpolation.

+7

[Follow Up 5 of 5] * DynamicObject * dynamic More importantly, you may want to learn about the Dynamic Language Runtime (DLR) and how it relates with the Common Language Runtime (CLR). https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/dynamic-language-runtime-overview It might not be a bad idea to make your way to the CLR .NET Fundamentals as well as reviewed in the links (and related sublinks below): https://docs.microsoft.com/en-us/dotnet/standard/clr https://docs.microsoft.com/en-us/dotnet/standard/common-type-system https://docs.microsoft.com/en-us/dotnet/standard/parallel-processing-and-concurrency https://docs.microsoft.com/en-us/dotnet/fundamentals/ These are the links I would focus on in your spare time. You'll revisit the articles embedded here many times over through the years and reach new epiphanies and discoveries while gaining new insights. IDE Intellisense should cover most of what you need regarding the .NET Standard Library.

+6

I am not totally sure what you are doing to be honest. If you are trying to access a property that does not exist in the class definition your code should not compile. Unless you are using reflection, or `dynamic`? An easier solution might be, to give each object a field called `bools` which is a `Dictionary<string, bool>` and then you can do dictionary lookup. That being said you might want to look into ExpandoObjects, maybe that fits your usecase. You can also write classes that extend `DynamicObject` but that's almost never the right solution.

+6

[Follow Up 2 of 5] -------- * base() -------- Inherited class members that are protected, internal, or public can be directly accessed via the base keyword. This is in contrast to the this keyword with accesses the immediate class members and any base members that aren't overridden. base() is used to call the base constructor from within the immediate classes constructor. Although class constructors can't be overridden, classes can reuse their base class constructors using this special method. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/base#example-1 -------- * "object" --> System.Object, right? -------- Yes... these are the same and are interchangeable. C# supports aliases for a set of Built-in .NET types. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types

+6

[Follow Up 3 of 5] -------- * Also the reversed class order its a bit confusing. -------- I'm not sure if you are referring to the order in which the classes are defined in the file. If this what you're referring to, then the order doesn't matter. The C# compiler automatically resolves the class dependency tree when compiling to CIL bytecode. Also, each class would typically be defined in their own respective files. In this case, order wouldn't be something that could be controlled anyway. Having multiple classes in a single file makes no difference to the compiler. It all gets sorted out. For clarity, the inheritance hierarchy in this code is as follows: System.Object (The root base class in the type hierarchy.) ----> System.Dynamic.DynamicObject --------> SoloLearn.DynamicProps ------------> SoloLearn.Animal ----------------> SoloLearn.Bird ----------------> SoloLearn.Person

+6

[Follow Up 4 of 5] -------- * Seem that what I need is know the standard library better, but as is huge, must learn under case necessity -------- Nah... These are more related to the C# language itself. I do recommend you spend time reviewing C# as a language without getting distracted with the standard .NET library. The link below is a great place to focus on just the C# language features. Pay special attention to the side nav links as these provide a great way to quickly jump around and explore the different features. No need to read everything or memorize. Just get familiar with what's available and revisit over time as needed. https://docs.microsoft.com/en-us/dotnet/csharp/ These links (and their related sub links) should cover most of what you should eventually be familiar with. ---- https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/ https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/

+3

David Carroll thx 4 the feedback new concepts 4 me: * @$"" * DynamicObject *dynamic *base() Also the reversed class order its a bit confusing. "object" --> System.Object, right? Seem that what I need is know the standard library better, but as is huge, must learn under case necessity

+2

https://code.sololearn.com/cYv9h8O2jXbc/?ref=app From outside the object, or in it?

+2

David Carroll thx 4 the feedback I know of the existence of the official C# site, got it on PDF. Anyways thx 4 the links, it helps. my problem is that Im doing many things at the same time; learning 3D modelling, learning a 3D engine and its scripting language, agnostic programming and other things like the Quake engine family standards, project design, and some others. So I think the best is learning pills, a bit everyday. The DLR-CLR matters seems to be enough relevant to jump-in rightnow. thx for your time, you're my C# mentor!

0

🌀PresidentOfYes12🌀 both, mainly outside on C#

0

Yes. Ill try to develop outside mainly, then go to inside